Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
46.97% covered (danger)
46.97%
890 / 1895
14.29% covered (danger)
14.29%
5 / 35
CRAP
0.00% covered (danger)
0.00%
0 / 1
AppController
46.88% covered (danger)
46.88%
887 / 1892
14.29% covered (danger)
14.29%
5 / 35
56932.10
0.00% covered (danger)
0.00%
0 / 1
 processSGF
98.11% covered (success)
98.11%
52 / 53
0.00% covered (danger)
0.00%
0 / 1
15
 xFlip
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 yFlip
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 getInitialPositionEnd
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 getInitialPosition
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
6
 getInvisibleSets
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 getDeletedSets
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 startPageUpdate
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
5.03
 deleteUnusedStatuses
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
72
 saveSolvedNumber
78.12% covered (warning)
78.12%
25 / 32
0.00% covered (danger)
0.00%
0 / 1
13.51
 resetUserElos
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 handleContribution
52.94% covered (warning)
52.94%
9 / 17
0.00% covered (danger)
0.00%
0 / 1
2.42
 getAllTags
96.55% covered (success)
96.55%
28 / 29
0.00% covered (danger)
0.00%
0 / 1
7
 encrypt
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 checkPictureLarge
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 checkPicture
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 saveDanSolveCondition
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
7
 updateSprintCondition
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
4.00
 updateGoldenCondition
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
3.00
 setPotionCondition
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
6
 updateGems
3.85% covered (danger)
3.85%
4 / 104
0.00% covered (danger)
0.00%
0 / 1
2183.48
 checkProblemNumberAchievements
42.39% covered (danger)
42.39%
39 / 92
0.00% covered (danger)
0.00%
0 / 1
166.38
 checkDanSolveAchievements
46.82% covered (danger)
46.82%
81 / 173
0.00% covered (danger)
0.00%
0 / 1
545.62
 checkForLocked
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 checkNoErrorAchievements
50.77% covered (warning)
50.77%
33 / 65
0.00% covered (danger)
0.00%
0 / 1
51.48
 checkTimeModeAchievements
35.40% covered (danger)
35.40%
40 / 113
0.00% covered (danger)
0.00%
0 / 1
2273.83
 checkRatingAchievements
54.44% covered (warning)
54.44%
49 / 90
0.00% covered (danger)
0.00%
0 / 1
95.92
 checkLevelAchievements
72.22% covered (warning)
72.22%
65 / 90
0.00% covered (danger)
0.00%
0 / 1
42.63
 checkSetCompletedAchievements
19.72% covered (danger)
19.72%
14 / 71
0.00% covered (danger)
0.00%
0 / 1
166.54
 setAchievementSpecial
0.00% covered (danger)
0.00%
0 / 184
0.00% covered (danger)
0.00%
0 / 1
4160
 checkSetAchievements
18.10% covered (danger)
18.10%
40 / 221
0.00% covered (danger)
0.00%
0 / 1
2533.09
 updateXP
60.00% covered (warning)
60.00%
6 / 10
0.00% covered (danger)
0.00%
0 / 1
3.58
 handleSearchSettings
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
 signIn
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 beforeFilter
85.40% covered (warning)
85.40%
275 / 322
0.00% covered (danger)
0.00%
0 / 1
78.14
 afterFilter
n/a
0 / 0
n/a
0 / 0
1
1<?php
2
3App::uses('Auth', 'Utility');
4App::uses('TsumegoFilters', 'Utility');
5App::uses('TimeMode', 'Utility');
6
7class AppController extends Controller {
8    public $viewClass = 'App';
9
10    public $components = [
11        'Session',
12        //'DebugKit.Toolbar',
13        'Flash',
14        'PlayResultProcessor'
15    ];
16
17    public static function processSGF($sgf) {
18        $aw = strpos($sgf, 'AW');
19        $ab = strpos($sgf, 'AB');
20        $boardSizePos = strpos($sgf, 'SZ');
21        $boardSize = 19;
22        $sgfArr = str_split($sgf);
23        if ($boardSizePos !== false) {
24            $boardSize = $sgfArr[$boardSizePos + 3] . '' . $sgfArr[$boardSizePos + 4];
25        }
26        if (substr($boardSize, 1) == ']') {
27            $boardSize = substr($boardSize, 0, 1);
28        }
29
30        $black = AppController::getInitialPosition($ab, $sgfArr, 'x');
31        $white = AppController::getInitialPosition($aw, $sgfArr, 'o');
32        $stones = array_merge($black, $white);
33
34        $board = [];
35        for ($i = 0; $i < 19; $i++) {
36            $board[$i] = [];
37            for ($j = 0; $j < 19; $j++) {
38                $board[$i][$j] = '-';
39            }
40        }
41        $lowestX = 18;
42        $lowestY = 18;
43        $highestX = 0;
44        $highestY = 0;
45        $stonesCount = count($stones);
46        for ($i = 0; $i < $stonesCount; $i++) {
47            if ($stones[$i][0] < $lowestX) {
48                $lowestX = $stones[$i][0];
49            }
50            if ($stones[$i][0] > $highestX) {
51                $highestX = $stones[$i][0];
52            }
53            if ($stones[$i][1] < $lowestY) {
54                $lowestY = $stones[$i][1];
55            }
56            if ($stones[$i][1] > $highestY) {
57                $highestY = $stones[$i][1];
58            }
59        }
60        if (18 - $lowestX < $lowestX) {
61            $stones = AppController::xFlip($stones);
62        }
63        if (18 - $lowestY < $lowestY) {
64            $stones = AppController::yFlip($stones);
65        }
66        $highestX = 0;
67        $highestY = 0;
68        $stonesCount = count($stones);
69        for ($i = 0; $i < $stonesCount; $i++) {
70            if ($stones[$i][0] > $highestX) {
71                $highestX = $stones[$i][0];
72            }
73            if ($stones[$i][1] > $highestY) {
74                $highestY = $stones[$i][1];
75            }
76            $board[$stones[$i][0]][$stones[$i][1]] = $stones[$i][2];
77        }
78        $tInfo = [];
79        $tInfo[0] = $highestX;
80        $tInfo[1] = $highestY;
81        $arr = [];
82        $arr[0] = $board;
83        $arr[1] = $stones;
84        $arr[2] = $tInfo;
85        $arr[3] = $boardSize;
86
87        return $arr;
88    }
89
90    public static function xFlip($stones) {
91        $stonesCount = count($stones);
92        for ($i = 0; $i < $stonesCount; $i++) {
93            $stones[$i][0] = 18 - $stones[$i][0];
94        }
95
96        return $stones;
97    }
98
99    public static function yFlip($stones) {
100        $stonesCount = count($stones);
101        for ($i = 0; $i < $stonesCount; $i++) {
102            $stones[$i][1] = 18 - $stones[$i][1];
103        }
104
105        return $stones;
106    }
107
108    public static function getInitialPositionEnd($pos, $sgfArr) {
109        $endCondition = 0;
110        $currentPos1 = $pos + 2;
111        $currentPos2 = $pos + 5;
112        while ($sgfArr[$currentPos1] == '[' && $sgfArr[$currentPos2] == ']') {
113            $endCondition = $currentPos2;
114            $currentPos1 += 4;
115            $currentPos2 += 4;
116        }
117
118        return $endCondition;
119    }
120
121    public static function getInitialPosition($pos, $sgfArr, $color) {
122        $arr = [];
123        $end = AppController::getInitialPositionEnd($pos, $sgfArr);
124        for ($i = $pos + 2; $i < $end; $i++) {
125            if ($sgfArr[$i] != '[' && $sgfArr[$i] != ']') {
126                array_push($arr, strtolower($sgfArr[$i]));
127            }
128        }
129        $alphabet = array_flip(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']);
130        $xy = true;
131        $arr2 = [];
132        $c = 0;
133        $arrCount = count($arr);
134        for ($i = 0; $i < $arrCount; $i++) {
135            $arr[$i] = $alphabet[$arr[$i]];
136            if ($xy) {
137                $arr2[$c] = [];
138                $arr2[$c][0] = $arr[$i];
139            } else {
140                $arr2[$c][1] = $arr[$i];
141                $arr2[$c][2] = $color;
142                $c++;
143            }
144            $xy = !$xy;
145        }
146
147        return $arr2;
148    }
149
150    protected function getInvisibleSets() {
151        $this->loadModel('Set');
152        $invisibleSets = [];
153        $in = $this->Set->find('all', ['conditions' => ['public' => 0]]);
154        if (!$in) {
155            $in = [];
156        }
157        foreach ($in as $item) {
158            $invisibleSets[] = $item['Set']['id'];
159        }
160
161        return $invisibleSets;
162    }
163
164    protected function getDeletedSets() {
165        $dSets = [];
166        $de = $this->Set->find('all', ['conditions' => ['public' => -1]]);
167        if (!$de) {
168            $de = [];
169        }
170        foreach ($de as $item) {
171            $dSets[] = $item['Set']['id'];
172        }
173
174        return $dSets;
175    }
176
177    /**
178     * @return void
179     */
180    public static function startPageUpdate() {
181        $str = '';
182        $latest = ClassRegistry::init('AchievementStatus')->find('all', ['limit' => 7, 'order' => 'created DESC']);
183        if (!$latest) {
184            $latest = [];
185        }
186        $latestCount = count($latest);
187        for ($i = 0; $i < $latestCount; $i++) {
188            $a = ClassRegistry::init('Achievement')->findById($latest[$i]['AchievementStatus']['achievement_id']);
189            $u = ClassRegistry::init('User')->findById($latest[$i]['AchievementStatus']['user_id']);
190            if (substr($u['User']['name'], 0, 3) == 'g__' && $u['User']['external_id'] != null) {
191                $startPageUser = AppController::checkPicture($u);
192            } else {
193                $startPageUser = $u['User']['name'];
194            }
195            $latest[$i]['AchievementStatus']['name'] = $a['Achievement']['name'];
196            $latest[$i]['AchievementStatus']['color'] = $a['Achievement']['color'];
197            $latest[$i]['AchievementStatus']['image'] = $a['Achievement']['image'];
198            $latest[$i]['AchievementStatus']['user'] = $startPageUser;
199            $str .= '<div class="quote1"><div class="quote1a"><a href="/achievements/view/' . $a['Achievement']['id'] . '"><img src="/img/' . $a['Achievement']['image'] . '.png" width="34px"></a></div>';
200            $str .= '<div class="quote1b">Achievement gained by ' . $startPageUser . ':<br><div class=""><b>' . $a['Achievement']['name'] . '</b></div></div></div>';
201        }
202        file_put_contents('mainPageAjax.txt', $str);
203    }
204
205    public static function deleteUnusedStatuses(int $uid): void {
206        $s = ClassRegistry::init('Set')->find('all', [
207            'conditions' => [
208                'OR' => [
209                    ['public' => 1],
210                    ['public' => 0],
211                ],
212            ],
213        ]) ?: [];
214        $ids = [];
215        $sCount = count($s);
216        for ($i = 0; $i < $sCount; $i++) {
217            $tSet = TsumegoUtil::collectTsumegosFromSet($s[$i]['Set']['id']);
218            foreach ($tSet as $item) {
219                $ids[] = $item['Tsumego']['id'];
220            }
221        }
222        $ut = ClassRegistry::init('TsumegoStatus')->find('all', [
223            'conditions' => [
224                'user_id' => $uid,
225                'NOT' => [
226                    'tsumego_id' => $ids,
227                ],
228            ],
229        ]);
230        if (!$ut) {
231            $ut = [];
232        }
233        $utCount = count($ut);
234        for ($i = 0; $i < $utCount; $i++) {
235            $test1 = ClassRegistry::init('Tsumego')->findById($ut[$i]['TsumegoStatus']['tsumego_id']);
236            $test2 = ClassRegistry::init('SetConnection')->find('first', ['conditions' => ['tsumego_id' => $test1['Tsumego']['id']]]);
237            if (!$test2) {
238                ClassRegistry::init('TsumegoStatus')->delete($ut[$i]['TsumegoStatus']['id']);
239
240                continue;
241            }
242            $test3 = ClassRegistry::init('Set')->find('first', [
243                'id' => $test2['SetConnection']['set_id'],
244                'OR' => [
245                    ['public' => 1],
246                    ['public' => 0],
247                ],
248            ]);
249            if ($test3 == null) {
250                ClassRegistry::init('TsumegoStatus')->delete($ut[$i]['TsumegoStatus']['id']);
251            }
252        }
253    }
254
255    protected function saveSolvedNumber($uid) {
256        $this->loadModel('User');
257        $this->loadModel('TsumegoStatus');
258        $this->loadModel('Set');
259        $this->loadModel('SetConnection');
260
261        $solvedUts2 = 0;
262        $tsumegos = $this->SetConnection->find('all');
263        if (!$tsumegos) {
264            $tsumegos = [];
265        }
266        $uts = $this->TsumegoStatus->find('all', ['order' => 'updated DESC', 'conditions' => ['user_id' => $uid]]);
267        if (!$uts) {
268            $uts = [];
269        }
270        $setKeys = [];
271        $setArray = $this->Set->find('all', ['conditions' => ['public' => 1]]);
272        if (!$setArray) {
273            $setArray = [];
274        }
275
276        $setArrayCount = count($setArray);
277        for ($i = 0; $i < $setArrayCount; $i++) {
278            $setKeys[$setArray[$i]['Set']['id']] = $setArray[$i]['Set']['id'];
279        }
280
281        $scs = [];
282        $tsumegosCount = count($tsumegos);
283        for ($j = 0; $j < $tsumegosCount; $j++) {
284            if (!isset($scs[$tsumegos[$j]['SetConnection']['tsumego_id']])) {
285                $scs[$tsumegos[$j]['SetConnection']['tsumego_id']] = 1;
286            } else {
287                $scs[$tsumegos[$j]['SetConnection']['tsumego_id']]++;
288            }
289        }
290        $utsCount = count($uts);
291        for ($j = 0; $j < $utsCount; $j++) {
292            if ($uts[$j]['TsumegoStatus']['status'] == 'S' || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C') {
293                if (isset($scs[$uts[$j]['TsumegoStatus']['tsumego_id']])) {
294                    $solvedUts2 += $scs[$uts[$j]['TsumegoStatus']['tsumego_id']];
295                }
296            }
297        }
298        Auth::getUser()['solved'] = $solvedUts2;
299        Auth::saveUser();
300
301        return $solvedUts2;
302    }
303
304    /**
305     * @return void
306     */
307    protected function resetUserElos() {
308        $this->loadModel('User');
309
310        $u = $this->User->find('all', [
311            'conditions' => [
312                'id >=' => 15000,
313                'id <=' => 19000,
314            ],
315        ]);
316        if (!$u) {
317            $u = [];
318        }
319
320        $uCount = count($u);
321        for ($i = 0; $i < $uCount; $i++) {
322            $u[$i]['User']['rating'] = 900;
323            $u[$i]['User']['solved2'] = 0;
324            $this->User->save($u[$i]);
325        }
326    }
327
328    /**
329     * @param int $uid User ID
330     * @param string $action Action type
331     *
332     * @return void
333     */
334    public static function handleContribution($uid, $action) {
335        $uc = ClassRegistry::init('UserContribution')->find('first', ['conditions' => ['user_id' => $uid]]);
336        if ($uc == null) {
337            $uc = [];
338            $uc['UserContribution']['user_id'] = $uid;
339            $uc['UserContribution']['added_tag'] = 0;
340            $uc['UserContribution']['created_tag'] = 0;
341            $uc['UserContribution']['made_proposal'] = 0;
342            $uc['UserContribution']['reviewed'] = 0;
343            $uc['UserContribution']['score'] = 0;
344            ClassRegistry::init('UserContribution')->create();
345        }
346        $uc['UserContribution'][$action] += 1;
347        $uc['UserContribution']['score']
348        = $uc['UserContribution']['added_tag']
349        + $uc['UserContribution']['created_tag'] * 3
350        + $uc['UserContribution']['made_proposal'] * 5
351        + $uc['UserContribution']['reviewed'] * 2;
352        ClassRegistry::init('UserContribution')->save($uc);
353    }
354
355    public static function getAllTags($not) {
356        $a = [];
357        $notApproved = ClassRegistry::init('TagName')->find('all', ['conditions' => ['approved' => 0]]);
358        if (!$notApproved) {
359            $notApproved = [];
360        }
361        $notCount = count($not);
362        for ($i = 0; $i < $notCount; $i++) {
363            array_push($a, $not[$i]['Tag']['tag_name_id']);
364        }
365        $notApprovedCount = count($notApproved);
366        for ($i = 0; $i < $notApprovedCount; $i++) {
367            array_push($a, $notApproved[$i]['TagName']['id']);
368        }
369        $tn = ClassRegistry::init('TagName')->find('all', [
370            'conditions' => [
371                'NOT' => ['id' => $a],
372            ],
373        ]);
374        if (!$tn) {
375            $tn = [];
376        }
377        $sorted = [];
378        $keys = [];
379        $tnCount = count($tn);
380        for ($i = 0; $i < $tnCount; $i++) {
381            array_push($sorted, $tn[$i]['TagName']['name']);
382            $keys[$tn[$i]['TagName']['name']] = $tn[$i];
383        }
384        sort($sorted);
385        $s2 = [];
386        $sortedCount = count($sorted);
387        for ($i = 0; $i < $sortedCount; $i++) {
388            array_push($s2, $keys[$sorted[$i]]);
389        }
390
391        return $s2;
392    }
393
394    public static function encrypt($str = null) {
395        $secret_key = 'my_simple_secret_keyx';
396        $secret_iv = 'my_simple_secret_ivx';
397        $encrypt_method = 'AES-256-CBC';
398        $key = hash('sha256', $secret_key);
399        $iv = substr(hash('sha256', $secret_iv), 0, 16);
400
401        return base64_encode(openssl_encrypt($str, $encrypt_method, $key, 0, $iv));
402    }
403
404    protected function checkPictureLarge($u) {
405        if (substr($u['User']['name'], 0, 3) == 'g__' && $u['User']['external_id'] != null) {
406            return '<img class="google-profile-image-large" src="/img/google/' . $u['User']['picture'] . '">' . substr($u['User']['name'], 3);
407        }
408
409        return $u['User']['name'];
410    }
411    public static function checkPicture($user) {
412        if (substr($user['name'], 0, 3) == 'g__' && $user['external_id'] != null) {
413            return '<img class="google-profile-image" src="/img/google/' . $user['picture'] . '">' . substr($user['name'], 3);
414        }
415
416        return $user['name'];
417    }
418
419    public static function saveDanSolveCondition($solvedTsumegoRank, $tId): void {
420        if ($solvedTsumegoRank == '1d' || $solvedTsumegoRank == '2d' || $solvedTsumegoRank == '3d' || $solvedTsumegoRank == '4d' || $solvedTsumegoRank == '5d') {
421            $danSolveCategory = 'danSolve' . $solvedTsumegoRank;
422            $danSolveCondition = ClassRegistry::init('AchievementCondition')->find('first', [
423                'order' => 'value DESC',
424                'conditions' => [
425                    'user_id' => Auth::getUserID(),
426                    'category' => $danSolveCategory,
427                ],
428            ]);
429            if (!$danSolveCondition) {
430                $danSolveCondition = [];
431                $danSolveCondition['AchievementCondition']['value'] = 0;
432                ClassRegistry::init('AchievementCondition')->create();
433            }
434            $danSolveCondition['AchievementCondition']['category'] = $danSolveCategory;
435            $danSolveCondition['AchievementCondition']['user_id'] = Auth::getUserID();
436            $danSolveCondition['AchievementCondition']['set_id'] = $tId;
437            $danSolveCondition['AchievementCondition']['value']++;
438
439            ClassRegistry::init('AchievementCondition')->save($danSolveCondition);
440        }
441    }
442
443    public static function updateSprintCondition(bool $trigger = false): void {
444        if (Auth::isLoggedIn()) {
445            $sprintCondition = ClassRegistry::init('AchievementCondition')->find('first', [
446                'order' => 'value DESC',
447                'conditions' => [
448                    'user_id' => Auth::getUserID(),
449                    'category' => 'sprint',
450                ],
451            ]);
452            if (!$sprintCondition) {
453                $sprintCondition = [];
454                $sprintCondition['AchievementCondition']['value'] = 0;
455                ClassRegistry::init('AchievementCondition')->create();
456            }
457            $sprintCondition['AchievementCondition']['category'] = 'sprint';
458            $sprintCondition['AchievementCondition']['user_id'] = Auth::getUserID();
459            if ($trigger) {
460                $sprintCondition['AchievementCondition']['value']++;
461            } else {
462                $sprintCondition['AchievementCondition']['value'] = 0;
463            }
464            ClassRegistry::init('AchievementCondition')->save($sprintCondition);
465        }
466    }
467
468    public static function updateGoldenCondition(bool $trigger = false): void {
469        $goldenCondition = ClassRegistry::init('AchievementCondition')->find('first', [
470            'order' => 'value DESC',
471            'conditions' => [
472                'user_id' => Auth::getUserID(),
473                'category' => 'golden',
474            ],
475        ]);
476        if (!$goldenCondition) {
477            $goldenCondition = [];
478            $goldenCondition['AchievementCondition']['value'] = 0;
479            ClassRegistry::init('AchievementCondition')->create();
480        }
481        $goldenCondition['AchievementCondition']['category'] = 'golden';
482        $goldenCondition['AchievementCondition']['user_id'] = Auth::getUserID();
483        if ($trigger) {
484            $goldenCondition['AchievementCondition']['value']++;
485        } else {
486            $goldenCondition['AchievementCondition']['value'] = 0;
487        }
488        ClassRegistry::init('AchievementCondition')->save($goldenCondition);
489    }
490
491    public static function setPotionCondition(): void {
492        $potionCondition = ClassRegistry::init('AchievementCondition')->find('first', [
493            'order' => 'value DESC',
494            'conditions' => [
495                'user_id' => Auth::getUserID(),
496                'category' => 'potion',
497            ],
498        ]);
499        if (!$potionCondition) {
500            $potionCondition = [];
501            ClassRegistry::init('AchievementCondition')->create();
502        }
503        $potionCondition['AchievementCondition']['category'] = 'potion';
504        $potionCondition['AchievementCondition']['user_id'] = Auth::getUserID();
505        $potionCondition['AchievementCondition']['value'] = 1;
506        ClassRegistry::init('AchievementCondition')->save($potionCondition);
507    }
508
509    public static function updateGems(string $rank): void {
510        $datex = new DateTime('today');
511        $dateGem = ClassRegistry::init('DayRecord')->find('first', ['conditions' => ['date' => $datex->format('Y-m-d')]]);
512        if ($dateGem != null) {
513            $gems = explode('-', $dateGem['DayRecord']['gems']);
514            $gemValue = '';
515            $gemValue2 = '';
516            $gemValue3 = '';
517            $condition1 = 500;
518            $condition2 = 200;
519            $condition3 = 5;
520            $found1 = false;
521            $found2 = false;
522            $found3 = false;
523            if ($rank == '15k' || $rank == '14k' || $rank == '13k' || $rank == '12k' || $rank == '11k' || $rank == '10k') {
524                if ($gems[0] == 0) {
525                    $gemValue = '15k';
526                } elseif ($gems[0] == 1) {
527                    $gemValue = '12k';
528                } elseif ($gems[0] == 2) {
529                    $gemValue = '10k';
530                }
531                if ($rank == $gemValue) {
532                    $dateGem['DayRecord']['gemCounter1']++;
533                    if ($dateGem['DayRecord']['gemCounter1'] == $condition1) {
534                        $found1 = true;
535                    }
536                }
537            } elseif ($rank == '9k' || $rank == '8k' || $rank == '7k' || $rank == '6k' || $rank == '5k' || $rank == '4k' || $rank == '3k' || $rank == '2k' || $rank == '1k') {
538                if ($gems[1] == 0) {
539                    $gemValue = '9k';
540                    $gemValue2 = 'x';
541                    $gemValue3 = 'y';
542                } elseif ($gems[1] == 1) {
543                    $gemValue = '6k';
544                    $gemValue2 = '5k';
545                    $gemValue3 = '4k';
546                } elseif ($gems[1] == 2) {
547                    $gemValue = 'x';
548                    $gemValue2 = '2k';
549                    $gemValue3 = '1k';
550                }
551                if ($rank == $gemValue || $rank == $gemValue2 || $rank == $gemValue3) {
552                    $dateGem['DayRecord']['gemCounter2']++;
553                    if ($dateGem['DayRecord']['gemCounter2'] == $condition2) {
554                        $found2 = true;
555                    }
556                }
557            } elseif ($rank == '1d' || $rank == '2d' || $rank == '3d' || $rank == '4d' || $rank == '5d' || $rank == '6d' || $rank == '7d') {
558                if ($gems[2] == 0) {
559                    $gemValue = '1d';
560                    $gemValue2 = '2d';
561                    $gemValue3 = '3d';
562                } elseif ($gems[2] == 1) {
563                    $gemValue = '2d';
564                    $gemValue2 = '3d';
565                    $gemValue3 = '4d';
566                } elseif ($gems[2] == 2) {
567                    $gemValue = '5d';
568                    $gemValue2 = '6d';
569                    $gemValue3 = '7d';
570                }
571                if ($rank == $gemValue || $rank == $gemValue2 || $rank == $gemValue3) {
572                    $dateGem['DayRecord']['gemCounter3']++;
573                    if ($dateGem['DayRecord']['gemCounter3'] == $condition3) {
574                        $found3 = true;
575                    }
576                }
577            }
578            if ($found1) {
579                $aCondition = ClassRegistry::init('AchievementCondition')->find('first', [
580                    'order' => 'value DESC',
581                    'conditions' => [
582                        'user_id' => Auth::getUserID(),
583                        'category' => 'emerald',
584                    ],
585                ]);
586                if ($aCondition == null) {
587                    $aCondition = [];
588                    $aCondition['AchievementCondition']['category'] = 'emerald';
589                    $aCondition['AchievementCondition']['user_id'] = Auth::getUserID();
590                    $aCondition['AchievementCondition']['value'] = 1;
591                    ClassRegistry::init('AchievementCondition')->save($aCondition);
592                } else {
593                    $dateGem['DayRecord']['gemCounter1']--;
594                }
595            } elseif ($found2) {
596                $aCondition = ClassRegistry::init('AchievementCondition')->find('first', [
597                    'order' => 'value DESC',
598                    'conditions' => [
599                        'user_id' => Auth::getUserID(),
600                        'category' => 'sapphire',
601                    ],
602                ]);
603                if ($aCondition == null) {
604                    $aCondition = [];
605                    $aCondition['AchievementCondition']['category'] = 'sapphire';
606                    $aCondition['AchievementCondition']['user_id'] = Auth::getUserID();
607                    $aCondition['AchievementCondition']['value'] = 1;
608                    ClassRegistry::init('AchievementCondition')->save($aCondition);
609                } else {
610                    $dateGem['DayRecord']['gemCounter2']--;
611                }
612            } elseif ($found3) {
613                $aCondition = ClassRegistry::init('AchievementCondition')->find('first', [
614                    'order' => 'value DESC',
615                    'conditions' => [
616                        'user_id' => Auth::getUserID(),
617                        'category' => 'ruby',
618                    ],
619                ]);
620                if ($aCondition == null) {
621                    $aCondition = [];
622                    $aCondition['AchievementCondition']['category'] = 'ruby';
623                    $aCondition['AchievementCondition']['user_id'] = Auth::getUserID();
624                    $aCondition['AchievementCondition']['value'] = 1;
625                    ClassRegistry::init('AchievementCondition')->save($aCondition);
626                } else {
627                    $dateGem['DayRecord']['gemCounter3']--;
628                }
629            }
630        }
631        ClassRegistry::init('DayRecord')->save($dateGem);
632    }
633
634    public static function checkProblemNumberAchievements() {
635        if (!Auth::isLoggedIn()) {
636            return;
637        }
638
639        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
640        if (!$buffer) {
641            $buffer = [];
642        }
643        $existingAs = [];
644        $bufferCount = count($buffer);
645        for ($i = 0; $i < $bufferCount; $i++) {
646            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
647        }
648        $as = [];
649        $as['AchievementStatus']['user_id'] = Auth::getUserID();
650        $updated = [];
651
652        $achievementId = 1;
653        $solvedCount = Auth::getUser()['solved'];
654        if ($solvedCount >= 1000 && !isset($existingAs[$achievementId])) {
655            $as['AchievementStatus']['achievement_id'] = $achievementId;
656            ClassRegistry::init('AchievementStatus')->create();
657            ClassRegistry::init('AchievementStatus')->save($as);
658            array_push($updated, $achievementId);
659        }
660        $achievementId = 2;
661        if ($solvedCount >= 2000 && !isset($existingAs[$achievementId])) {
662            $as['AchievementStatus']['achievement_id'] = $achievementId;
663            ClassRegistry::init('AchievementStatus')->create();
664            ClassRegistry::init('AchievementStatus')->save($as);
665            array_push($updated, $achievementId);
666        }
667        $achievementId = 3;
668        if ($solvedCount >= 3000 && !isset($existingAs[$achievementId])) {
669            $as['AchievementStatus']['achievement_id'] = $achievementId;
670            ClassRegistry::init('AchievementStatus')->create();
671            ClassRegistry::init('AchievementStatus')->save($as);
672            array_push($updated, $achievementId);
673        }
674        $achievementId = 4;
675        if ($solvedCount >= 4000 && !isset($existingAs[$achievementId])) {
676            $as['AchievementStatus']['achievement_id'] = $achievementId;
677            ClassRegistry::init('AchievementStatus')->create();
678            ClassRegistry::init('AchievementStatus')->save($as);
679            array_push($updated, $achievementId);
680        }
681        $achievementId = 5;
682        if ($solvedCount >= 5000 && !isset($existingAs[$achievementId])) {
683            $as['AchievementStatus']['achievement_id'] = $achievementId;
684            ClassRegistry::init('AchievementStatus')->create();
685            ClassRegistry::init('AchievementStatus')->save($as);
686            array_push($updated, $achievementId);
687        }
688        $achievementId = 6;
689        if ($solvedCount >= 6000 && !isset($existingAs[$achievementId])) {
690            $as['AchievementStatus']['achievement_id'] = $achievementId;
691            ClassRegistry::init('AchievementStatus')->create();
692            ClassRegistry::init('AchievementStatus')->save($as);
693            array_push($updated, $achievementId);
694        }
695        $achievementId = 7;
696        if ($solvedCount >= 7000 && !isset($existingAs[$achievementId])) {
697            $as['AchievementStatus']['achievement_id'] = $achievementId;
698            ClassRegistry::init('AchievementStatus')->create();
699            ClassRegistry::init('AchievementStatus')->save($as);
700            array_push($updated, $achievementId);
701        }
702        $achievementId = 8;
703        if ($solvedCount >= 8000 && !isset($existingAs[$achievementId])) {
704            $as['AchievementStatus']['achievement_id'] = $achievementId;
705            ClassRegistry::init('AchievementStatus')->create();
706            ClassRegistry::init('AchievementStatus')->save($as);
707            array_push($updated, $achievementId);
708        }
709        $achievementId = 9;
710        if ($solvedCount >= 9000 && !isset($existingAs[$achievementId])) {
711            $as['AchievementStatus']['achievement_id'] = $achievementId;
712            ClassRegistry::init('AchievementStatus')->create();
713            ClassRegistry::init('AchievementStatus')->save($as);
714            array_push($updated, $achievementId);
715        }
716        $achievementId = 10;
717        if ($solvedCount >= 10000 && !isset($existingAs[$achievementId])) {
718            $as['AchievementStatus']['achievement_id'] = $achievementId;
719            ClassRegistry::init('AchievementStatus')->create();
720            ClassRegistry::init('AchievementStatus')->save($as);
721            array_push($updated, $achievementId);
722        }
723        //uotd achievement
724        $achievementId = 11;
725        if (!isset($existingAs[$achievementId])) {
726            $condition = ClassRegistry::init('AchievementCondition')->find('first', ['conditions' => ['user_id' => Auth::getUserID(), 'category' => 'uotd']]);
727            if ($condition != null) {
728                $as['AchievementStatus']['achievement_id'] = $achievementId;
729                ClassRegistry::init('AchievementStatus')->create();
730                ClassRegistry::init('AchievementStatus')->save($as);
731                array_push($updated, $achievementId);
732            }
733        }
734
735        $updatedCount = count($updated);
736        for ($i = 0; $i < $updatedCount; $i++) {
737            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
738            $updated[$i] = [];
739            $updated[$i][0] = $a['Achievement']['name'];
740            $updated[$i][1] = $a['Achievement']['description'];
741            $updated[$i][2] = $a['Achievement']['image'];
742            $updated[$i][3] = $a['Achievement']['color'];
743            $updated[$i][4] = $a['Achievement']['xp'];
744            $updated[$i][5] = $a['Achievement']['id'];
745        }
746
747        return $updated;
748    }
749
750    public static function checkDanSolveAchievements() {
751        if (Auth::isLoggedIn()) {
752            $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
753            if (!$buffer) {
754                $buffer = [];
755            }
756            $ac = ClassRegistry::init('AchievementCondition')->find('all', [
757                'order' => 'category ASC',
758                'conditions' => [
759                    'user_id' => Auth::getUserID(),
760                    'OR' => [
761                        ['category' => 'danSolve1d'],
762                        ['category' => 'danSolve2d'],
763                        ['category' => 'danSolve3d'],
764                        ['category' => 'danSolve4d'],
765                        ['category' => 'danSolve5d'],
766                        ['category' => 'emerald'],
767                        ['category' => 'sapphire'],
768                        ['category' => 'ruby'],
769                        ['category' => 'sprint'],
770                        ['category' => 'golden'],
771                        ['category' => 'potion'],
772                    ],
773                ],
774            ]);
775            if (!$ac) {
776                $ac = [];
777            }
778            $ac1 = [];
779            $acCount = count($ac);
780            for ($i = 0; $i < $acCount; $i++) {
781                if ($ac[$i]['AchievementCondition']['category'] == 'danSolve1d') {
782                    $ac1['1d'] = $ac[$i]['AchievementCondition']['value'];
783                } elseif ($ac[$i]['AchievementCondition']['category'] == 'danSolve2d') {
784                    $ac1['2d'] = $ac[$i]['AchievementCondition']['value'];
785                } elseif ($ac[$i]['AchievementCondition']['category'] == 'danSolve3d') {
786                    $ac1['3d'] = $ac[$i]['AchievementCondition']['value'];
787                } elseif ($ac[$i]['AchievementCondition']['category'] == 'danSolve4d') {
788                    $ac1['4d'] = $ac[$i]['AchievementCondition']['value'];
789                } elseif ($ac[$i]['AchievementCondition']['category'] == 'danSolve5d') {
790                    $ac1['5d'] = $ac[$i]['AchievementCondition']['value'];
791                } elseif ($ac[$i]['AchievementCondition']['category'] == 'emerald') {
792                    $ac1['emerald'] = $ac[$i]['AchievementCondition']['value'];
793                } elseif ($ac[$i]['AchievementCondition']['category'] == 'sapphire') {
794                    $ac1['sapphire'] = $ac[$i]['AchievementCondition']['value'];
795                } elseif ($ac[$i]['AchievementCondition']['category'] == 'ruby') {
796                    $ac1['ruby'] = $ac[$i]['AchievementCondition']['value'];
797                } elseif ($ac[$i]['AchievementCondition']['category'] == 'sprint') {
798                    $ac1['sprint'] = $ac[$i]['AchievementCondition']['value'];
799                } elseif ($ac[$i]['AchievementCondition']['category'] == 'golden') {
800                    $ac1['golden'] = $ac[$i]['AchievementCondition']['value'];
801                } elseif ($ac[$i]['AchievementCondition']['category'] == 'potion') {
802                    $ac1['potion'] = $ac[$i]['AchievementCondition']['value'];
803                }
804            }
805
806            $existingAs = [];
807            $bufferCount = count($buffer);
808            for ($i = 0; $i < $bufferCount; $i++) {
809                $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
810            }
811            $as = [];
812            $as['AchievementStatus']['user_id'] = Auth::getUserID();
813            $updated = [];
814            $achievementId = 101;
815            if ($ac1['1d'] > 0 && !isset($existingAs[$achievementId])) {
816                $as['AchievementStatus']['achievement_id'] = $achievementId;
817                ClassRegistry::init('AchievementStatus')->create();
818                ClassRegistry::init('AchievementStatus')->save($as);
819                array_push($updated, $achievementId);
820            }
821            $achievementId = 102;
822            if ($ac1['2d'] > 0 && !isset($existingAs[$achievementId])) {
823                $as['AchievementStatus']['achievement_id'] = $achievementId;
824                ClassRegistry::init('AchievementStatus')->create();
825                ClassRegistry::init('AchievementStatus')->save($as);
826                array_push($updated, $achievementId);
827            }
828            $achievementId = 103;
829            if ($ac1['3d'] > 0 && !isset($existingAs[$achievementId])) {
830                $as['AchievementStatus']['achievement_id'] = $achievementId;
831                ClassRegistry::init('AchievementStatus')->create();
832                ClassRegistry::init('AchievementStatus')->save($as);
833                array_push($updated, $achievementId);
834            }
835            $achievementId = 104;
836            if ($ac1['4d'] > 0 && !isset($existingAs[$achievementId])) {
837                $as['AchievementStatus']['achievement_id'] = $achievementId;
838                ClassRegistry::init('AchievementStatus')->create();
839                ClassRegistry::init('AchievementStatus')->save($as);
840                array_push($updated, $achievementId);
841            }
842            $achievementId = 105;
843            if ($ac1['5d'] > 0 && !isset($existingAs[$achievementId])) {
844                $as['AchievementStatus']['achievement_id'] = $achievementId;
845                ClassRegistry::init('AchievementStatus')->create();
846                ClassRegistry::init('AchievementStatus')->save($as);
847                array_push($updated, $achievementId);
848            }
849            $achievementId = 106;
850            if ($ac1['1d'] >= 10 && !isset($existingAs[$achievementId])) {
851                $as['AchievementStatus']['achievement_id'] = $achievementId;
852                ClassRegistry::init('AchievementStatus')->create();
853                ClassRegistry::init('AchievementStatus')->save($as);
854                array_push($updated, $achievementId);
855            }
856            $achievementId = 107;
857            if ($ac1['2d'] >= 10 && !isset($existingAs[$achievementId])) {
858                $as['AchievementStatus']['achievement_id'] = $achievementId;
859                ClassRegistry::init('AchievementStatus')->create();
860                ClassRegistry::init('AchievementStatus')->save($as);
861                array_push($updated, $achievementId);
862            }
863            $achievementId = 108;
864            if ($ac1['3d'] >= 10 && !isset($existingAs[$achievementId])) {
865                $as['AchievementStatus']['achievement_id'] = $achievementId;
866                ClassRegistry::init('AchievementStatus')->create();
867                ClassRegistry::init('AchievementStatus')->save($as);
868                array_push($updated, $achievementId);
869            }
870            $achievementId = 109;
871            if ($ac1['4d'] >= 10 && !isset($existingAs[$achievementId])) {
872                $as['AchievementStatus']['achievement_id'] = $achievementId;
873                ClassRegistry::init('AchievementStatus')->create();
874                ClassRegistry::init('AchievementStatus')->save($as);
875                array_push($updated, $achievementId);
876            }
877            $achievementId = 110;
878            if ($ac1['5d'] >= 10 && !isset($existingAs[$achievementId])) {
879                $as['AchievementStatus']['achievement_id'] = $achievementId;
880                ClassRegistry::init('AchievementStatus')->create();
881                ClassRegistry::init('AchievementStatus')->save($as);
882                array_push($updated, $achievementId);
883            }
884            $achievementId = 111;
885            if (isset($ac1['emerald'])) {
886                if ($ac1['emerald'] == 1 && !isset($existingAs[$achievementId])) {
887                    $as['AchievementStatus']['achievement_id'] = $achievementId;
888                    ClassRegistry::init('AchievementStatus')->create();
889                    ClassRegistry::init('AchievementStatus')->save($as);
890                    array_push($updated, $achievementId);
891                }
892            }
893            $achievementId = 112;
894            if (isset($ac1['sapphire'])) {
895                if ($ac1['sapphire'] == 1 && !isset($existingAs[$achievementId])) {
896                    $as['AchievementStatus']['achievement_id'] = $achievementId;
897                    ClassRegistry::init('AchievementStatus')->create();
898                    ClassRegistry::init('AchievementStatus')->save($as);
899                    array_push($updated, $achievementId);
900                }
901            }
902            $achievementId = 113;
903            if (isset($ac1['ruby'])) {
904                if ($ac1['ruby'] == 1 && !isset($existingAs[$achievementId])) {
905                    $as['AchievementStatus']['achievement_id'] = $achievementId;
906                    ClassRegistry::init('AchievementStatus')->create();
907                    ClassRegistry::init('AchievementStatus')->save($as);
908                    array_push($updated, $achievementId);
909                }
910            }
911            $achievementId = 114;
912            if (!isset($existingAs[$achievementId]) && isset($existingAs[111]) && isset($existingAs[112]) && isset($existingAs[113])) {
913                $as['AchievementStatus']['achievement_id'] = $achievementId;
914                ClassRegistry::init('AchievementStatus')->create();
915                ClassRegistry::init('AchievementStatus')->save($as);
916                array_push($updated, $achievementId);
917            }
918            $achievementId = 96;
919            if (!isset($existingAs[$achievementId]) && $ac1['sprint'] >= 30) {
920                $as['AchievementStatus']['achievement_id'] = $achievementId;
921                ClassRegistry::init('AchievementStatus')->create();
922                ClassRegistry::init('AchievementStatus')->save($as);
923                array_push($updated, $achievementId);
924            }
925            $achievementId = 97;
926            if (!isset($existingAs[$achievementId]) && $ac1['golden'] >= 10) {
927                $as['AchievementStatus']['achievement_id'] = $achievementId;
928                ClassRegistry::init('AchievementStatus')->create();
929                ClassRegistry::init('AchievementStatus')->save($as);
930                array_push($updated, $achievementId);
931            }
932            $achievementId = 98;
933            if (!isset($existingAs[$achievementId]) && $ac1['potion'] >= 1) {
934                $as['AchievementStatus']['achievement_id'] = $achievementId;
935                ClassRegistry::init('AchievementStatus')->create();
936                ClassRegistry::init('AchievementStatus')->save($as);
937                array_push($updated, $achievementId);
938            }
939            $updatedCount = count($updated);
940            for ($i = 0; $i < $updatedCount; $i++) {
941                $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
942                $updated[$i] = [];
943                $updated[$i][0] = $a['Achievement']['name'];
944                $updated[$i][1] = $a['Achievement']['description'];
945                $updated[$i][2] = $a['Achievement']['image'];
946                $updated[$i][3] = $a['Achievement']['color'];
947                $updated[$i][4] = $a['Achievement']['xp'];
948                $updated[$i][5] = $a['Achievement']['id'];
949            }
950
951            return $updated;
952        }
953    }
954
955    protected function checkForLocked($t, $setsWithPremium) {
956        $scCheck = $this->SetConnection->find('first', ['conditions' => ['tsumego_id' => $t['Tsumego']['id']]]);
957        if ($scCheck && in_array($scCheck['SetConnection']['set_id'], $setsWithPremium) && !Auth::hasPremium()) {
958            $t['Tsumego']['locked'] = true;
959        } else {
960            $t['Tsumego']['locked'] = false;
961        }
962
963        return $t;
964    }
965    public static function checkNoErrorAchievements() {
966        if (Auth::isLoggedIn()) {
967
968            $ac = ClassRegistry::init('AchievementCondition')->find('first', [
969                'order' => 'value DESC',
970                'conditions' => [
971                    'user_id' => Auth::getUserID(),
972                    'category' => 'err',
973                ],
974            ]);
975
976            $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
977            if (!$buffer) {
978                $buffer = [];
979            }
980            $existingAs = [];
981            $bufferCount = count($buffer);
982            for ($i = 0; $i < $bufferCount; $i++) {
983                $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
984            }
985            $as = [];
986            $as['AchievementStatus']['user_id'] = Auth::getUserID();
987            $updated = [];
988
989            $achievementId = 53;
990            if ($ac['AchievementCondition']['value'] >= 10 && !isset($existingAs[$achievementId])) {
991                $as['AchievementStatus']['achievement_id'] = $achievementId;
992                ClassRegistry::init('AchievementStatus')->create();
993                ClassRegistry::init('AchievementStatus')->save($as);
994                array_push($updated, $achievementId);
995            }
996            $achievementId = 54;
997            if ($ac['AchievementCondition']['value'] >= 20 && !isset($existingAs[$achievementId])) {
998                $as['AchievementStatus']['achievement_id'] = $achievementId;
999                ClassRegistry::init('AchievementStatus')->create();
1000                ClassRegistry::init('AchievementStatus')->save($as);
1001                array_push($updated, $achievementId);
1002            }
1003            $achievementId = 55;
1004            if ($ac['AchievementCondition']['value'] >= 30 && !isset($existingAs[$achievementId])) {
1005                $as['AchievementStatus']['achievement_id'] = $achievementId;
1006                ClassRegistry::init('AchievementStatus')->create();
1007                ClassRegistry::init('AchievementStatus')->save($as);
1008                array_push($updated, $achievementId);
1009            }
1010            $achievementId = 56;
1011            if ($ac['AchievementCondition']['value'] >= 50 && !isset($existingAs[$achievementId])) {
1012                $as['AchievementStatus']['achievement_id'] = $achievementId;
1013                ClassRegistry::init('AchievementStatus')->create();
1014                ClassRegistry::init('AchievementStatus')->save($as);
1015                array_push($updated, $achievementId);
1016            }
1017            $achievementId = 57;
1018            if ($ac['AchievementCondition']['value'] >= 100 && !isset($existingAs[$achievementId])) {
1019                $as['AchievementStatus']['achievement_id'] = $achievementId;
1020                ClassRegistry::init('AchievementStatus')->create();
1021                ClassRegistry::init('AchievementStatus')->save($as);
1022                array_push($updated, $achievementId);
1023            }
1024            $achievementId = 58;
1025            if ($ac['AchievementCondition']['value'] >= 200 && !isset($existingAs[$achievementId])) {
1026                $as['AchievementStatus']['achievement_id'] = $achievementId;
1027                ClassRegistry::init('AchievementStatus')->create();
1028                ClassRegistry::init('AchievementStatus')->save($as);
1029                array_push($updated, $achievementId);
1030            }
1031            $updatedCount = count($updated);
1032            for ($i = 0; $i < $updatedCount; $i++) {
1033                $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
1034                $updated[$i] = [];
1035                $updated[$i][0] = $a['Achievement']['name'];
1036                $updated[$i][1] = $a['Achievement']['description'];
1037                $updated[$i][2] = $a['Achievement']['image'];
1038                $updated[$i][3] = $a['Achievement']['color'];
1039                $updated[$i][4] = $a['Achievement']['xp'];
1040                $updated[$i][5] = $a['Achievement']['id'];
1041            }
1042
1043            return $updated;
1044        }
1045    }
1046
1047    protected function checkTimeModeAchievements() {
1048        $this->loadModel('Achievement');
1049        $this->loadModel('AchievementStatus');
1050        $this->loadModel('TimeModeSession');
1051
1052        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
1053        if (!$buffer) {
1054            $buffer = [];
1055        }
1056        $existingAs = [];
1057        $bufferCount = count($buffer);
1058        for ($i = 0; $i < $bufferCount; $i++) {
1059            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
1060        }
1061        $as = [];
1062        $as['AchievementStatus']['user_id'] = Auth::getUserID();
1063        $updated = [];
1064
1065        $rBlitz = $this->TimeModeSession->find('all', ['conditions' => ['time_mode_category_id' => TimeModeUtil::$CATEGORY_BLITZ, 'user_id' => Auth::getUserID()]]);
1066        if (!$rBlitz) {
1067            $rBlitz = [];
1068        }
1069        $rFast = $this->TimeModeSession->find('all', ['conditions' => ['time_mode_category_id' => TimeModeUtil::$CATEGORY_FAST_SPEED, 'user_id' => Auth::getUserID()]]);
1070        if (!$rFast) {
1071            $rFast = [];
1072        }
1073        $rSlow = $this->TimeModeSession->find('all', ['conditions' => ['time_mode_category_id' => TimeModeUtil::$CATEGORY_SLOW_SPEED, 'user_id' => Auth::getUserID()]]);
1074        if (!$rSlow) {
1075            $rSlow = [];
1076        }
1077        $r = $this->TimeModeSession->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
1078        if (!$r) {
1079            $r = [];
1080        }
1081
1082        $timeModeAchievements = [];
1083        for ($i = 70; $i <= 91; $i++) {
1084            $timeModeAchievements[$i] = false;
1085        }
1086        $rCount = count($r);
1087        for ($i = 0; $i < $rCount; $i++) {
1088            if ($r[$i]['TimeModeSession']['status'] == 's') {
1089                if ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '5k') {
1090                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
1091                        $timeModeAchievements[70] = true;
1092                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
1093                        $timeModeAchievements[76] = true;
1094                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
1095                        $timeModeAchievements[82] = true;
1096                    }
1097                } elseif ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '4k') {
1098                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
1099                        $timeModeAchievements[71] = true;
1100                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
1101                        $timeModeAchievements[77] = true;
1102                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
1103                        $timeModeAchievements[83] = true;
1104                    }
1105                } elseif ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '3k') {
1106                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
1107                        $timeModeAchievements[72] = true;
1108                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
1109                        $timeModeAchievements[78] = true;
1110                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
1111                        $timeModeAchievements[84] = true;
1112                    }
1113                } elseif ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '2k') {
1114                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
1115                        $timeModeAchievements[73] = true;
1116                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
1117                        $timeModeAchievements[79] = true;
1118                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
1119                        $timeModeAchievements[85] = true;
1120                    }
1121                } elseif ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '1k') {
1122                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
1123                        $timeModeAchievements[74] = true;
1124                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
1125                        $timeModeAchievements[80] = true;
1126                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
1127                        $timeModeAchievements[86] = true;
1128                    }
1129                } elseif ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '1d') {
1130                    if ($r[$i]['TimeModeSession']['mode'] == 2) {
1131                        $timeModeAchievements[75] = true;
1132                    } elseif ($r[$i]['TimeModeSession']['mode'] == 1) {
1133                        $timeModeAchievements[81] = true;
1134                    } elseif ($r[$i]['TimeModeSession']['mode'] == 0) {
1135                        $timeModeAchievements[87] = true;
1136                    }
1137                }
1138            }
1139            if ($r[$i]['TimeModeSession']['points'] >= 850
1140            && ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '4k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1k'
1141            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '4d'
1142            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5d')) {
1143                $timeModeAchievements[91] = true;
1144            }
1145            if ($r[$i]['TimeModeSession']['points'] >= 875
1146            && ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '4k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1k'
1147            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '4d'
1148            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '6k')) {
1149                $timeModeAchievements[90] = true;
1150            }
1151            if ($r[$i]['TimeModeSession']['points'] >= 900
1152            && ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '4k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1k'
1153            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '4d'
1154            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '6k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '7k'
1155            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '8k')) {
1156                $timeModeAchievements[89] = true;
1157            }
1158            if ($r[$i]['TimeModeSession']['points'] >= 950
1159            && ($r[$i]['TimeModeSession']['TimeModeAttempt'] == '4k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1k'
1160            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '1d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '2d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '3d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '4d'
1161            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5d' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '5k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '6k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '7k'
1162            || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '8k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '9k' || $r[$i]['TimeModeSession']['TimeModeAttempt'] == '10k')) {
1163                $timeModeAchievements[88] = true;
1164            }
1165        }
1166        for ($i = 70; $i <= 91; $i++) {
1167            $achievementId = $i;
1168            if ($timeModeAchievements[$achievementId] == true && !isset($existingAs[$achievementId])) {
1169                $as['AchievementStatus']['achievement_id'] = $achievementId;
1170                ClassRegistry::init('AchievementStatus')->create();
1171                ClassRegistry::init('AchievementStatus')->save($as);
1172                array_push($updated, $achievementId);
1173            }
1174        }
1175        $updatedCount = count($updated);
1176        for ($i = 0; $i < $updatedCount; $i++) {
1177            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
1178            $updated[$i] = [];
1179            $updated[$i][0] = $a['Achievement']['name'];
1180            $updated[$i][1] = $a['Achievement']['description'];
1181            $updated[$i][2] = $a['Achievement']['image'];
1182            $updated[$i][3] = $a['Achievement']['color'];
1183            $updated[$i][4] = $a['Achievement']['xp'];
1184            $updated[$i][5] = $a['Achievement']['id'];
1185        }
1186
1187        return $updated;
1188    }
1189
1190    public static function checkRatingAchievements() {
1191        if (!Auth::isLoggedIn()) {
1192            return;
1193        }
1194
1195        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
1196        if (!$buffer) {
1197            $buffer = [];
1198        }
1199        $existingAs = [];
1200        $bufferCount = count($buffer);
1201        for ($i = 0; $i < $bufferCount; $i++) {
1202            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
1203        }
1204        $as = [];
1205        $as['AchievementStatus']['user_id'] = Auth::getUserID();
1206        $updated = [];
1207
1208        $achievementId = 59;
1209        $currentElo = Auth::getUser()['rating'];
1210        if ($currentElo >= 1500 && !isset($existingAs[$achievementId])) {
1211            $as['AchievementStatus']['achievement_id'] = $achievementId;
1212            ClassRegistry::init('AchievementStatus')->create();
1213            ClassRegistry::init('AchievementStatus')->save($as);
1214            array_push($updated, $achievementId);
1215        }
1216        $achievementId = 60;
1217        if ($currentElo >= 1600 && !isset($existingAs[$achievementId])) {
1218            $as['AchievementStatus']['achievement_id'] = $achievementId;
1219            ClassRegistry::init('AchievementStatus')->create();
1220            ClassRegistry::init('AchievementStatus')->save($as);
1221            array_push($updated, $achievementId);
1222        }
1223        $achievementId = 61;
1224        if ($currentElo >= 1700 && !isset($existingAs[$achievementId])) {
1225            $as['AchievementStatus']['achievement_id'] = $achievementId;
1226            ClassRegistry::init('AchievementStatus')->create();
1227            ClassRegistry::init('AchievementStatus')->save($as);
1228            array_push($updated, $achievementId);
1229        }
1230        $achievementId = 62;
1231        if ($currentElo >= 1800 && !isset($existingAs[$achievementId])) {
1232            $as['AchievementStatus']['achievement_id'] = $achievementId;
1233            ClassRegistry::init('AchievementStatus')->create();
1234            ClassRegistry::init('AchievementStatus')->save($as);
1235            array_push($updated, $achievementId);
1236        }
1237        $achievementId = 63;
1238        if ($currentElo >= 1900 && !isset($existingAs[$achievementId])) {
1239            $as['AchievementStatus']['achievement_id'] = $achievementId;
1240            ClassRegistry::init('AchievementStatus')->create();
1241            ClassRegistry::init('AchievementStatus')->save($as);
1242            array_push($updated, $achievementId);
1243        }
1244        $achievementId = 64;
1245        if ($currentElo >= 2000 && !isset($existingAs[$achievementId])) {
1246            $as['AchievementStatus']['achievement_id'] = $achievementId;
1247            ClassRegistry::init('AchievementStatus')->create();
1248            ClassRegistry::init('AchievementStatus')->save($as);
1249            array_push($updated, $achievementId);
1250        }
1251        $achievementId = 65;
1252        if ($currentElo >= 2100 && !isset($existingAs[$achievementId])) {
1253            $as['AchievementStatus']['achievement_id'] = $achievementId;
1254            ClassRegistry::init('AchievementStatus')->create();
1255            ClassRegistry::init('AchievementStatus')->save($as);
1256            array_push($updated, $achievementId);
1257        }
1258        $achievementId = 66;
1259        if ($currentElo >= 2200 && !isset($existingAs[$achievementId])) {
1260            $as['AchievementStatus']['achievement_id'] = $achievementId;
1261            ClassRegistry::init('AchievementStatus')->create();
1262            ClassRegistry::init('AchievementStatus')->save($as);
1263            array_push($updated, $achievementId);
1264        }
1265        $achievementId = 67;
1266        if ($currentElo >= 2300 && !isset($existingAs[$achievementId])) {
1267            $as['AchievementStatus']['achievement_id'] = $achievementId;
1268            ClassRegistry::init('AchievementStatus')->create();
1269            ClassRegistry::init('AchievementStatus')->save($as);
1270            array_push($updated, $achievementId);
1271        }
1272        $achievementId = 68;
1273        if ($currentElo >= 2400 && !isset($existingAs[$achievementId])) {
1274            $as['AchievementStatus']['achievement_id'] = $achievementId;
1275            ClassRegistry::init('AchievementStatus')->create();
1276            ClassRegistry::init('AchievementStatus')->save($as);
1277            array_push($updated, $achievementId);
1278        }
1279        $achievementId = 69;
1280        if ($currentElo >= 2500 && !isset($existingAs[$achievementId])) {
1281            $as['AchievementStatus']['achievement_id'] = $achievementId;
1282            ClassRegistry::init('AchievementStatus')->create();
1283            ClassRegistry::init('AchievementStatus')->save($as);
1284            array_push($updated, $achievementId);
1285        }
1286        $updatedCount = count($updated);
1287        for ($i = 0; $i < $updatedCount; $i++) {
1288            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
1289            $updated[$i] = [];
1290            $updated[$i][0] = $a['Achievement']['name'];
1291            $updated[$i][1] = $a['Achievement']['description'];
1292            $updated[$i][2] = $a['Achievement']['image'];
1293            $updated[$i][3] = $a['Achievement']['color'];
1294            $updated[$i][4] = $a['Achievement']['xp'];
1295            $updated[$i][5] = $a['Achievement']['id'];
1296        }
1297
1298        return $updated;
1299    }
1300
1301    public static function checkLevelAchievements() {
1302        if (!Auth::isLoggedIn()) {
1303            return;
1304        }
1305        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
1306        if (!$buffer) {
1307            $buffer = [];
1308        }
1309        $existingAs = [];
1310        $bufferCount = count($buffer);
1311        for ($i = 0; $i < $bufferCount; $i++) {
1312            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
1313        }
1314        $as = [];
1315        $as['AchievementStatus']['user_id'] = Auth::getUserID();
1316        $updated = [];
1317
1318        $achievementId = 36;
1319        $userLevel = Auth::getUser()['level'];
1320        if ($userLevel >= 10 && !isset($existingAs[$achievementId])) {
1321            $as['AchievementStatus']['achievement_id'] = $achievementId;
1322            ClassRegistry::init('AchievementStatus')->create();
1323            ClassRegistry::init('AchievementStatus')->save($as);
1324            array_push($updated, $achievementId);
1325        }
1326        $achievementId = 37;
1327        if ($userLevel >= 20 && !isset($existingAs[$achievementId])) {
1328            $as['AchievementStatus']['achievement_id'] = $achievementId;
1329            ClassRegistry::init('AchievementStatus')->create();
1330            ClassRegistry::init('AchievementStatus')->save($as);
1331            array_push($updated, $achievementId);
1332        }
1333        $achievementId = 38;
1334        if ($userLevel >= 30 && !isset($existingAs[$achievementId])) {
1335            $as['AchievementStatus']['achievement_id'] = $achievementId;
1336            ClassRegistry::init('AchievementStatus')->create();
1337            ClassRegistry::init('AchievementStatus')->save($as);
1338            array_push($updated, $achievementId);
1339        }
1340        $achievementId = 39;
1341        if ($userLevel >= 40 && !isset($existingAs[$achievementId])) {
1342            $as['AchievementStatus']['achievement_id'] = $achievementId;
1343            ClassRegistry::init('AchievementStatus')->create();
1344            ClassRegistry::init('AchievementStatus')->save($as);
1345            array_push($updated, $achievementId);
1346        }
1347        $achievementId = 40;
1348        if ($userLevel >= 50 && !isset($existingAs[$achievementId])) {
1349            $as['AchievementStatus']['achievement_id'] = $achievementId;
1350            ClassRegistry::init('AchievementStatus')->create();
1351            ClassRegistry::init('AchievementStatus')->save($as);
1352            array_push($updated, $achievementId);
1353        }
1354        $achievementId = 41;
1355        if ($userLevel >= 60 && !isset($existingAs[$achievementId])) {
1356            $as['AchievementStatus']['achievement_id'] = $achievementId;
1357            ClassRegistry::init('AchievementStatus')->create();
1358            ClassRegistry::init('AchievementStatus')->save($as);
1359            array_push($updated, $achievementId);
1360        }
1361        $achievementId = 42;
1362        if ($userLevel >= 70 && !isset($existingAs[$achievementId])) {
1363            $as['AchievementStatus']['achievement_id'] = $achievementId;
1364            ClassRegistry::init('AchievementStatus')->create();
1365            ClassRegistry::init('AchievementStatus')->save($as);
1366            array_push($updated, $achievementId);
1367        }
1368        $achievementId = 43;
1369        if ($userLevel >= 80 && !isset($existingAs[$achievementId])) {
1370            $as['AchievementStatus']['achievement_id'] = $achievementId;
1371            ClassRegistry::init('AchievementStatus')->create();
1372            ClassRegistry::init('AchievementStatus')->save($as);
1373            array_push($updated, $achievementId);
1374        }
1375        $achievementId = 44;
1376        if ($userLevel >= 90 && !isset($existingAs[$achievementId])) {
1377            $as['AchievementStatus']['achievement_id'] = $achievementId;
1378            ClassRegistry::init('AchievementStatus')->create();
1379            ClassRegistry::init('AchievementStatus')->save($as);
1380            array_push($updated, $achievementId);
1381        }
1382        $achievementId = 45;
1383        if ($userLevel >= 100 && !isset($existingAs[$achievementId])) {
1384            $as['AchievementStatus']['achievement_id'] = $achievementId;
1385            ClassRegistry::init('AchievementStatus')->create();
1386            ClassRegistry::init('AchievementStatus')->save($as);
1387            array_push($updated, $achievementId);
1388        }
1389        $achievementId = 100;
1390        if (Auth::hasPremium() && !isset($existingAs[$achievementId])) {
1391            $as['AchievementStatus']['achievement_id'] = $achievementId;
1392            ClassRegistry::init('AchievementStatus')->create();
1393            ClassRegistry::init('AchievementStatus')->save($as);
1394            array_push($updated, $achievementId);
1395        }
1396        $updatedCount = count($updated);
1397        for ($i = 0; $i < $updatedCount; $i++) {
1398            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
1399            $updated[$i] = [];
1400            $updated[$i][0] = $a['Achievement']['name'];
1401            $updated[$i][1] = $a['Achievement']['description'];
1402            $updated[$i][2] = $a['Achievement']['image'];
1403            $updated[$i][3] = $a['Achievement']['color'];
1404            $updated[$i][4] = $a['Achievement']['xp'];
1405            $updated[$i][5] = $a['Achievement']['id'];
1406        }
1407
1408        return $updated;
1409    }
1410
1411    protected function checkSetCompletedAchievements() {
1412        $this->loadModel('Set');
1413        $this->loadModel('Tsumego');
1414        $this->loadModel('Achievement');
1415        $this->loadModel('AchievementStatus');
1416        $this->loadModel('AchievementCondition');
1417
1418        $ac = ClassRegistry::init('AchievementCondition')->find('first', [
1419            'order' => 'value DESC',
1420            'conditions' => [
1421                'user_id' => Auth::getUserID(),
1422                'category' => 'set',
1423            ],
1424        ]);
1425
1426        if (!$ac) {
1427            return [];
1428        }
1429
1430        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
1431        if (!$buffer) {
1432            $buffer = [];
1433        }
1434        $existingAs = [];
1435        $bufferCount = count($buffer);
1436        for ($i = 0; $i < $bufferCount; $i++) {
1437            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
1438        }
1439        $as = [];
1440        $as['AchievementStatus']['user_id'] = Auth::getUserID();
1441        $updated = [];
1442
1443        $achievementId = 47;
1444        if ($ac['AchievementCondition']['value'] >= 10 && !isset($existingAs[$achievementId])) {
1445            $as['AchievementStatus']['achievement_id'] = $achievementId;
1446            ClassRegistry::init('AchievementStatus')->create();
1447            ClassRegistry::init('AchievementStatus')->save($as);
1448            array_push($updated, $achievementId);
1449        }
1450        $achievementId = 48;
1451        if ($ac['AchievementCondition']['value'] >= 20 && !isset($existingAs[$achievementId])) {
1452            $as['AchievementStatus']['achievement_id'] = $achievementId;
1453            ClassRegistry::init('AchievementStatus')->create();
1454            ClassRegistry::init('AchievementStatus')->save($as);
1455            array_push($updated, $achievementId);
1456        }
1457        $achievementId = 49;
1458        if ($ac['AchievementCondition']['value'] >= 30 && !isset($existingAs[$achievementId])) {
1459            $as['AchievementStatus']['achievement_id'] = $achievementId;
1460            ClassRegistry::init('AchievementStatus')->create();
1461            ClassRegistry::init('AchievementStatus')->save($as);
1462            array_push($updated, $achievementId);
1463        }
1464        $achievementId = 50;
1465        if ($ac['AchievementCondition']['value'] >= 40 && !isset($existingAs[$achievementId])) {
1466            $as['AchievementStatus']['achievement_id'] = $achievementId;
1467            ClassRegistry::init('AchievementStatus')->create();
1468            ClassRegistry::init('AchievementStatus')->save($as);
1469            array_push($updated, $achievementId);
1470        }
1471        $achievementId = 51;
1472        if ($ac['AchievementCondition']['value'] >= 50 && !isset($existingAs[$achievementId])) {
1473            $as['AchievementStatus']['achievement_id'] = $achievementId;
1474            ClassRegistry::init('AchievementStatus')->create();
1475            ClassRegistry::init('AchievementStatus')->save($as);
1476            array_push($updated, $achievementId);
1477        }
1478        $achievementId = 52;
1479        if ($ac['AchievementCondition']['value'] >= 60 && !isset($existingAs[$achievementId])) {
1480            $as['AchievementStatus']['achievement_id'] = $achievementId;
1481            ClassRegistry::init('AchievementStatus')->create();
1482            ClassRegistry::init('AchievementStatus')->save($as);
1483            array_push($updated, $achievementId);
1484        }
1485        $updatedCount = count($updated);
1486        for ($i = 0; $i < $updatedCount; $i++) {
1487            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
1488            $updated[$i] = [];
1489            $updated[$i][0] = $a['Achievement']['name'];
1490            $updated[$i][1] = $a['Achievement']['description'];
1491            $updated[$i][2] = $a['Achievement']['image'];
1492            $updated[$i][3] = $a['Achievement']['color'];
1493            $updated[$i][4] = $a['Achievement']['xp'];
1494            $updated[$i][5] = $a['Achievement']['id'];
1495        }
1496
1497        return $updated;
1498    }
1499
1500    protected function setAchievementSpecial($s = null) {
1501        $this->loadModel('Set');
1502        $this->loadModel('Tsumego');
1503        $this->loadModel('TsumegoStatus');
1504        $this->loadModel('Achievement');
1505        $this->loadModel('AchievementStatus');
1506        $this->loadModel('SetConnection');
1507
1508        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
1509        if (!$buffer) {
1510            $buffer = [];
1511        }
1512        $existingAs = [];
1513        $bufferCount = count($buffer);
1514        for ($i = 0; $i < $bufferCount; $i++) {
1515            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
1516        }
1517        $as = [];
1518        $as['AchievementStatus']['user_id'] = Auth::getUserID();
1519        $updated = [];
1520
1521        $tsIds = [];
1522        $completed = '';
1523        if ($s == 'cc1') {
1524            $ts1 = TsumegoUtil::collectTsumegosFromSet(50);
1525            $ts2 = TsumegoUtil::collectTsumegosFromSet(52);
1526            $ts3 = TsumegoUtil::collectTsumegosFromSet(53);
1527            $ts4 = TsumegoUtil::collectTsumegosFromSet(54);
1528            $ts = array_merge($ts1, $ts2, $ts3, $ts4);
1529            $tsCount = count($ts);
1530            for ($i = 0; $i < $tsCount; $i++) {
1531                array_push($tsIds, $ts[$i]['Tsumego']['id']);
1532            }
1533            $uts = $this->TsumegoStatus->find('all', [
1534                'conditions' => [
1535                    'user_id' => Auth::getUserID(),
1536                    'tsumego_id' => $tsIds,
1537                ],
1538            ]);
1539            if (!$uts) {
1540                $uts = [];
1541            }
1542            $counter = 0;
1543            $utsCount = count($uts);
1544            for ($j = 0; $j < $utsCount; $j++) {
1545                for ($k = 0; $k < $tsCount; $k++) {
1546                    if ($uts[$j]['TsumegoStatus']['tsumego_id'] == $ts[$k]['Tsumego']['id'] && ($uts[$j]['TsumegoStatus']['status'] == 'S'
1547                    || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C')) {
1548                        $counter++;
1549                    }
1550                }
1551            }
1552            if ($counter == count($ts)) {
1553                $completed = $s;
1554            }
1555        } elseif ($s == 'cc2') {
1556            $ts1 = TsumegoUtil::collectTsumegosFromSet(41);
1557            $ts2 = TsumegoUtil::collectTsumegosFromSet(49);
1558            $ts3 = TsumegoUtil::collectTsumegosFromSet(65);
1559            $ts4 = TsumegoUtil::collectTsumegosFromSet(66);
1560            $ts = array_merge($ts1, $ts2, $ts3, $ts4);
1561            $tsCount = count($ts);
1562            for ($i = 0; $i < $tsCount; $i++) {
1563                array_push($tsIds, $ts[$i]['Tsumego']['id']);
1564            }
1565            $uts = $this->TsumegoStatus->find('all', [
1566                'conditions' => [
1567                    'user_id' => Auth::getUserID(),
1568                    'tsumego_id' => $tsIds,
1569                ],
1570            ]);
1571            if (!$uts) {
1572                $uts = [];
1573            }
1574            $counter = 0;
1575            $utsCount = count($uts);
1576            for ($j = 0; $j < $utsCount; $j++) {
1577                for ($k = 0; $k < $tsCount; $k++) {
1578                    if ($uts[$j]['TsumegoStatus']['tsumego_id'] == $ts[$k]['Tsumego']['id'] && ($uts[$j]['TsumegoStatus']['status'] == 'S'
1579                    || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C')) {
1580                        $counter++;
1581                    }
1582                }
1583            }
1584            if ($counter == count($ts)) {
1585                $completed = $s;
1586            }
1587        } elseif ($s == 'cc3') {
1588            $ts1 = TsumegoUtil::collectTsumegosFromSet(186);
1589            $ts2 = TsumegoUtil::collectTsumegosFromSet(187);
1590            $ts3 = TsumegoUtil::collectTsumegosFromSet(196);
1591            $ts4 = TsumegoUtil::collectTsumegosFromSet(203);
1592            $ts = array_merge($ts1, $ts2, $ts3, $ts4);
1593            $tsCount = count($ts);
1594            for ($i = 0; $i < $tsCount; $i++) {
1595                array_push($tsIds, $ts[$i]['Tsumego']['id']);
1596            }
1597            $uts = $this->TsumegoStatus->find('all', [
1598                'conditions' => [
1599                    'user_id' => Auth::getUserID(),
1600                    'tsumego_id' => $tsIds,
1601                ],
1602            ]);
1603            if (!$uts) {
1604                $uts = [];
1605            }
1606            $counter = 0;
1607            $utsCount = count($uts);
1608            for ($j = 0; $j < $utsCount; $j++) {
1609                for ($k = 0; $k < $tsCount; $k++) {
1610                    if ($uts[$j]['TsumegoStatus']['tsumego_id'] == $ts[$k]['Tsumego']['id'] && ($uts[$j]['TsumegoStatus']['status'] == 'S'
1611                    || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C')) {
1612                        $counter++;
1613                    }
1614                }
1615            }
1616            if ($counter == count($ts)) {
1617                $completed = $s;
1618            }
1619        } elseif ($s == '1000w1') {
1620            $ts1 = TsumegoUtil::collectTsumegosFromSet(190);
1621            $ts2 = TsumegoUtil::collectTsumegosFromSet(193);
1622            $ts3 = TsumegoUtil::collectTsumegosFromSet(198);
1623            $ts = array_merge($ts1, $ts2, $ts3);
1624            $tsCount = count($ts);
1625            for ($i = 0; $i < $tsCount; $i++) {
1626                array_push($tsIds, $ts[$i]['Tsumego']['id']);
1627            }
1628            $uts = $this->TsumegoStatus->find('all', [
1629                'conditions' => [
1630                    'user_id' => Auth::getUserID(),
1631                    'tsumego_id' => $tsIds,
1632                ],
1633            ]);
1634            if (!$uts) {
1635                $uts = [];
1636            }
1637            $counter = 0;
1638            $utsCount = count($uts);
1639            for ($j = 0; $j < $utsCount; $j++) {
1640                for ($k = 0; $k < $tsCount; $k++) {
1641                    if ($uts[$j]['TsumegoStatus']['tsumego_id'] == $ts[$k]['Tsumego']['id'] && ($uts[$j]['TsumegoStatus']['status'] == 'S'
1642                    || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C')) {
1643                        $counter++;
1644                    }
1645                }
1646            }
1647            if ($counter == count($ts)) {
1648                $completed = $s;
1649            }
1650        } elseif ($s == '1000w2') {
1651            $ts = TsumegoUtil::collectTsumegosFromSet(216);
1652            $tsCount = count($ts);
1653            for ($i = 0; $i < $tsCount; $i++) {
1654                array_push($tsIds, $ts[$i]['Tsumego']['id']);
1655            }
1656            $uts = $this->TsumegoStatus->find('all', [
1657                'conditions' => [
1658                    'user_id' => Auth::getUserID(),
1659                    'tsumego_id' => $tsIds,
1660                ],
1661            ]);
1662            if (!$uts) {
1663                $uts = [];
1664            }
1665            $counter = 0;
1666            $utsCount = count($uts);
1667            for ($j = 0; $j < $utsCount; $j++) {
1668                for ($k = 0; $k < $tsCount; $k++) {
1669                    if ($uts[$j]['TsumegoStatus']['tsumego_id'] == $ts[$k]['Tsumego']['id'] && ($uts[$j]['TsumegoStatus']['status'] == 'S'
1670                    || $uts[$j]['TsumegoStatus']['status'] == 'W' || $uts[$j]['TsumegoStatus']['status'] == 'C')) {
1671                        $counter++;
1672                    }
1673                }
1674            }
1675            if ($counter == count($ts)) {
1676                $completed = $s;
1677            }
1678        }
1679
1680        $achievementId = 92;
1681        if ($completed == 'cc1' && !isset($existingAs[$achievementId])) {
1682            $as['AchievementStatus']['achievement_id'] = $achievementId;
1683            ClassRegistry::init('AchievementStatus')->create();
1684            ClassRegistry::init('AchievementStatus')->save($as);
1685            array_push($updated, $achievementId);
1686        }
1687        $achievementId = 93;
1688        if ($completed == 'cc2' && !isset($existingAs[$achievementId])) {
1689            $as['AchievementStatus']['achievement_id'] = $achievementId;
1690            ClassRegistry::init('AchievementStatus')->create();
1691            ClassRegistry::init('AchievementStatus')->save($as);
1692            array_push($updated, $achievementId);
1693        }
1694        $achievementId = 94;
1695        if ($completed == 'cc3' && !isset($existingAs[$achievementId])) {
1696            $as['AchievementStatus']['achievement_id'] = $achievementId;
1697            ClassRegistry::init('AchievementStatus')->create();
1698            ClassRegistry::init('AchievementStatus')->save($as);
1699            array_push($updated, $achievementId);
1700        }
1701        $achievementId = 95;
1702        if ($completed == '1000w1' && !isset($existingAs[$achievementId])) {
1703            $as['AchievementStatus']['achievement_id'] = $achievementId;
1704            ClassRegistry::init('AchievementStatus')->create();
1705            ClassRegistry::init('AchievementStatus')->save($as);
1706            array_push($updated, $achievementId);
1707        }
1708        $achievementId = 115;
1709        if ($completed == '1000w2' && !isset($existingAs[$achievementId])) {
1710            $as['AchievementStatus']['achievement_id'] = $achievementId;
1711            ClassRegistry::init('AchievementStatus')->create();
1712            ClassRegistry::init('AchievementStatus')->save($as);
1713            array_push($updated, $achievementId);
1714        }
1715        $updatedCount = count($updated);
1716        for ($i = 0; $i < $updatedCount; $i++) {
1717            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
1718            $updated[$i] = [];
1719            $updated[$i][0] = $a['Achievement']['name'];
1720            $updated[$i][1] = $a['Achievement']['description'];
1721            $updated[$i][2] = $a['Achievement']['image'];
1722            $updated[$i][3] = $a['Achievement']['color'];
1723            $updated[$i][4] = $a['Achievement']['xp'];
1724            $updated[$i][5] = $a['Achievement']['id'];
1725        }
1726
1727        return $updated;
1728    }
1729
1730    protected function checkSetAchievements($sid = null) {
1731        $this->loadModel('Set');
1732        $this->loadModel('Tsumego');
1733        $this->loadModel('Achievement');
1734        $this->loadModel('AchievementStatus');
1735        $this->loadModel('AchievementCondition');
1736
1737        //$tNum = count($this->Tsumego->find('all', array('conditions' => array('set_id' => $sid))));
1738        $tNum = count(TsumegoUtil::collectTsumegosFromSet($sid));
1739        $s = $this->Set->findById($sid);
1740        $acA = ClassRegistry::init('AchievementCondition')->find('first', [
1741            'order' => 'value DESC',
1742            'conditions' => [
1743                'set_id' => $sid,
1744                'user_id' => Auth::getUserID(),
1745                'category' => '%',
1746            ],
1747        ]);
1748        if (!$acA) {
1749            return [];
1750        }
1751        $acS = ClassRegistry::init('AchievementCondition')->find('first', [
1752            'order' => 'value ASC',
1753            'conditions' => [
1754                'set_id' => $sid,
1755                'user_id' => Auth::getUserID(),
1756                'category' => 's',
1757            ],
1758        ]);
1759        $buffer = ClassRegistry::init('AchievementStatus')->find('all', ['conditions' => ['user_id' => Auth::getUserID()]]);
1760        if (!$buffer) {
1761            $buffer = [];
1762        }
1763        $existingAs = [];
1764        $bufferCount = count($buffer);
1765        for ($i = 0; $i < $bufferCount; $i++) {
1766            $existingAs[$buffer[$i]['AchievementStatus']['achievement_id']] = $buffer[$i];
1767        }
1768        $as = [];
1769        $as['AchievementStatus']['user_id'] = Auth::getUserID();
1770        $updated = [];
1771
1772        $achievementId = 99;
1773        if ($sid == -1 && !isset($existingAs[$achievementId])) {
1774            $as['AchievementStatus']['achievement_id'] = $achievementId;
1775            ClassRegistry::init('AchievementStatus')->create();
1776            ClassRegistry::init('AchievementStatus')->save($as);
1777            array_push($updated, $achievementId);
1778        }
1779        if ($tNum >= 100) {
1780            if ($s['Set']['difficulty'] < 1300) {
1781                $achievementId = 12;
1782                if ($acA['AchievementCondition']['value'] >= 75 && !isset($existingAs[$achievementId])) {
1783                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1784                    ClassRegistry::init('AchievementStatus')->create();
1785                    ClassRegistry::init('AchievementStatus')->save($as);
1786                    array_push($updated, $achievementId);
1787                }
1788                $achievementId = 13;
1789                if ($acA['AchievementCondition']['value'] >= 85 && !isset($existingAs[$achievementId])) {
1790                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1791                    ClassRegistry::init('AchievementStatus')->create();
1792                    ClassRegistry::init('AchievementStatus')->save($as);
1793                    array_push($updated, $achievementId);
1794                }
1795                $achievementId = 14;
1796                if ($acA['AchievementCondition']['value'] >= 95 && !isset($existingAs[$achievementId])) {
1797                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1798                    ClassRegistry::init('AchievementStatus')->create();
1799                    ClassRegistry::init('AchievementStatus')->save($as);
1800                    array_push($updated, $achievementId);
1801                }
1802                $achievementId = 24;
1803                if ($acS['AchievementCondition']['value'] < 15 && !isset($existingAs[$achievementId])) {
1804                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1805                    ClassRegistry::init('AchievementStatus')->create();
1806                    ClassRegistry::init('AchievementStatus')->save($as);
1807                    array_push($updated, $achievementId);
1808                }
1809                $achievementId = 25;
1810                if ($acS['AchievementCondition']['value'] < 10 && !isset($existingAs[$achievementId])) {
1811                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1812                    ClassRegistry::init('AchievementStatus')->create();
1813                    ClassRegistry::init('AchievementStatus')->save($as);
1814                    array_push($updated, $achievementId);
1815                }
1816                $achievementId = 26;
1817                if ($acS['AchievementCondition']['value'] < 5 && !isset($existingAs[$achievementId])) {
1818                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1819                    ClassRegistry::init('AchievementStatus')->create();
1820                    ClassRegistry::init('AchievementStatus')->save($as);
1821                    array_push($updated, $achievementId);
1822                }
1823            } elseif ($s['Set']['difficulty'] >= 1300 && $s['Set']['difficulty'] < 1500) {
1824                $achievementId = 15;
1825                if ($acA['AchievementCondition']['value'] >= 75 && !isset($existingAs[$achievementId])) {
1826                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1827                    ClassRegistry::init('AchievementStatus')->create();
1828                    ClassRegistry::init('AchievementStatus')->save($as);
1829                    array_push($updated, $achievementId);
1830                }
1831                $achievementId = 16;
1832                if ($acA['AchievementCondition']['value'] >= 85 && !isset($existingAs[$achievementId])) {
1833                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1834                    ClassRegistry::init('AchievementStatus')->create();
1835                    ClassRegistry::init('AchievementStatus')->save($as);
1836                    array_push($updated, $achievementId);
1837                }
1838                $achievementId = 17;
1839                if ($acA['AchievementCondition']['value'] >= 95 && !isset($existingAs[$achievementId])) {
1840                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1841                    ClassRegistry::init('AchievementStatus')->create();
1842                    ClassRegistry::init('AchievementStatus')->save($as);
1843                    array_push($updated, $achievementId);
1844                }
1845                $achievementId = 27;
1846                if ($acS['AchievementCondition']['value'] < 18 && !isset($existingAs[$achievementId])) {
1847                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1848                    ClassRegistry::init('AchievementStatus')->create();
1849                    ClassRegistry::init('AchievementStatus')->save($as);
1850                    array_push($updated, $achievementId);
1851                }
1852                $achievementId = 28;
1853                if ($acS['AchievementCondition']['value'] < 13 && !isset($existingAs[$achievementId])) {
1854                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1855                    ClassRegistry::init('AchievementStatus')->create();
1856                    ClassRegistry::init('AchievementStatus')->save($as);
1857                    array_push($updated, $achievementId);
1858                }
1859                $achievementId = 29;
1860                if ($acS['AchievementCondition']['value'] < 8 && !isset($existingAs[$achievementId])) {
1861                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1862                    ClassRegistry::init('AchievementStatus')->create();
1863                    ClassRegistry::init('AchievementStatus')->save($as);
1864                    array_push($updated, $achievementId);
1865                }
1866            } elseif ($s['Set']['difficulty'] >= 1500 && $s['Set']['difficulty'] < 1700) {
1867                $achievementId = 18;
1868                if ($acA['AchievementCondition']['value'] >= 75 && !isset($existingAs[$achievementId])) {
1869                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1870                    ClassRegistry::init('AchievementStatus')->create();
1871                    ClassRegistry::init('AchievementStatus')->save($as);
1872                    array_push($updated, $achievementId);
1873                }
1874                $achievementId = 19;
1875                if ($acA['AchievementCondition']['value'] >= 85 && !isset($existingAs[$achievementId])) {
1876                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1877                    ClassRegistry::init('AchievementStatus')->create();
1878                    ClassRegistry::init('AchievementStatus')->save($as);
1879                    array_push($updated, $achievementId);
1880                }
1881                $achievementId = 20;
1882                if ($acA['AchievementCondition']['value'] >= 95 && !isset($existingAs[$achievementId])) {
1883                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1884                    ClassRegistry::init('AchievementStatus')->create();
1885                    ClassRegistry::init('AchievementStatus')->save($as);
1886                    array_push($updated, $achievementId);
1887                }
1888                $achievementId = 30;
1889                if ($acS['AchievementCondition']['value'] < 30 && !isset($existingAs[$achievementId])) {
1890                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1891                    ClassRegistry::init('AchievementStatus')->create();
1892                    ClassRegistry::init('AchievementStatus')->save($as);
1893                    array_push($updated, $achievementId);
1894                }
1895                $achievementId = 31;
1896                if ($acS['AchievementCondition']['value'] < 20 && !isset($existingAs[$achievementId])) {
1897                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1898                    ClassRegistry::init('AchievementStatus')->create();
1899                    ClassRegistry::init('AchievementStatus')->save($as);
1900                    array_push($updated, $achievementId);
1901                }
1902                $achievementId = 32;
1903                if ($acS['AchievementCondition']['value'] < 10 && !isset($existingAs[$achievementId])) {
1904                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1905                    ClassRegistry::init('AchievementStatus')->create();
1906                    ClassRegistry::init('AchievementStatus')->save($as);
1907                    array_push($updated, $achievementId);
1908                }
1909            } else {
1910                $achievementId = 21;
1911                if ($acA['AchievementCondition']['value'] >= 75 && !isset($existingAs[$achievementId])) {
1912                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1913                    ClassRegistry::init('AchievementStatus')->create();
1914                    ClassRegistry::init('AchievementStatus')->save($as);
1915                    array_push($updated, $achievementId);
1916                }
1917                $achievementId = 22;
1918                if ($acA['AchievementCondition']['value'] >= 85 && !isset($existingAs[$achievementId])) {
1919                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1920                    ClassRegistry::init('AchievementStatus')->create();
1921                    ClassRegistry::init('AchievementStatus')->save($as);
1922                    array_push($updated, $achievementId);
1923                }
1924                $achievementId = 23;
1925                if ($acA['AchievementCondition']['value'] >= 95 && !isset($existingAs[$achievementId])) {
1926                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1927                    ClassRegistry::init('AchievementStatus')->create();
1928                    ClassRegistry::init('AchievementStatus')->save($as);
1929                    array_push($updated, $achievementId);
1930                }
1931                $achievementId = 33;
1932                if ($acS['AchievementCondition']['value'] < 30 && !isset($existingAs[$achievementId])) {
1933                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1934                    ClassRegistry::init('AchievementStatus')->create();
1935                    ClassRegistry::init('AchievementStatus')->save($as);
1936                    array_push($updated, $achievementId);
1937                }
1938                $achievementId = 34;
1939                if ($acS['AchievementCondition']['value'] < 20 && !isset($existingAs[$achievementId])) {
1940                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1941                    ClassRegistry::init('AchievementStatus')->create();
1942                    ClassRegistry::init('AchievementStatus')->save($as);
1943                    array_push($updated, $achievementId);
1944                }
1945                $achievementId = 35;
1946                if ($acS['AchievementCondition']['value'] < 10 && !isset($existingAs[$achievementId])) {
1947                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1948                    ClassRegistry::init('AchievementStatus')->create();
1949                    ClassRegistry::init('AchievementStatus')->save($as);
1950                    array_push($updated, $achievementId);
1951                }
1952            }
1953            $achievementId = 46;
1954            if ($acA['AchievementCondition']['value'] >= 100) {
1955                $ac100 = ClassRegistry::init('AchievementCondition')->find('all', ['conditions' => ['user_id' => Auth::getUserID(), 'category' => '%', 'value >=' => 100]]);
1956                if (!$ac100) {
1957                    $ac100 = [];
1958                }
1959                $ac100counter = 0;
1960                $ac100Count = count($ac100);
1961                for ($j = 0; $j < $ac100Count; $j++) {
1962                    if (count(TsumegoUtil::collectTsumegosFromSet($ac100[$j]['AchievementCondition']['set_id'])) >= 100) {
1963                        $ac100counter++;
1964                    }
1965                }
1966                $as100 = ClassRegistry::init('AchievementStatus')->find('first', ['conditions' => ['user_id' => Auth::getUserID(), 'achievement_id' => $achievementId]]);
1967                if ($as100 == null) {
1968                    $as['AchievementStatus']['achievement_id'] = $achievementId;
1969                    $as['AchievementStatus']['value'] = 1;
1970                    ClassRegistry::init('AchievementStatus')->create();
1971                    ClassRegistry::init('AchievementStatus')->save($as);
1972                    array_push($updated, $achievementId);
1973                } elseif ($as100['AchievementStatus']['value'] != $ac100counter) {
1974                    $as100['AchievementStatus']['value'] = $ac100counter;
1975                    ClassRegistry::init('AchievementStatus')->save($as100);
1976                    array_push($updated, $achievementId);
1977                }
1978            }
1979        }
1980        $updatedCount = count($updated);
1981        for ($i = 0; $i < $updatedCount; $i++) {
1982            $a = ClassRegistry::init('Achievement')->findById($updated[$i]);
1983            $updated[$i] = [];
1984            $updated[$i][0] = $a['Achievement']['name'];
1985            $updated[$i][1] = $a['Achievement']['description'];
1986            $updated[$i][2] = $a['Achievement']['image'];
1987            $updated[$i][3] = $a['Achievement']['color'];
1988            $updated[$i][4] = $a['Achievement']['xp'];
1989            $updated[$i][5] = $a['Achievement']['id'];
1990        }
1991
1992        return $updated;
1993    }
1994
1995    public static function updateXP($userID, $achievementData): void {
1996        $xpBonus = 0;
1997        $aCount = count($achievementData);
1998        for ($i = 0; $i < $aCount; $i++) {
1999            $xpBonus += $achievementData[$i][4];
2000        }
2001        if ($xpBonus == 0) {
2002            return;
2003        }
2004        $user = ClassRegistry::init('User')->findById($userID);
2005        $user['User']['xp'] = $xpBonus;
2006        Level::checkLevelUp($user['User']);
2007        ClassRegistry::init('User')->save($user);
2008    }
2009
2010    /**
2011     * @param int $uid User ID
2012     * @return void
2013     */
2014    protected function handleSearchSettings($uid) {
2015        $this->loadModel('UserContribution');
2016        $uc = $this->UserContribution->find('first', ['conditions' => ['user_id' => $uid]]);
2017        if ($uc == null) {
2018            $uc = [];
2019            $uc['UserContribution']['user_id'] = $uid;
2020            $uc['UserContribution']['added_tag'] = 0;
2021            $uc['UserContribution']['created_tag'] = 0;
2022            $uc['UserContribution']['made_proposal'] = 0;
2023            $uc['UserContribution']['reviewed'] = 0;
2024            $uc['UserContribution']['score'] = 0;
2025            $this->UserContribution->create();
2026            $this->UserContribution->save($uc);
2027        }
2028        new TsumegoFilters();
2029    }
2030
2031    protected function signIn(array $user): void {
2032        Auth::init($user);
2033        $vs = $this->TsumegoStatus->find('first', ['conditions' => ['user_id' => $user['User']['id']], 'order' => 'updated DESC']);
2034        if ($vs) {
2035            $this->Session->write('lastVisit', $vs['TsumegoStatus']['tsumego_id']);
2036        }
2037        $this->Session->write('texture', $user['User']['texture']);
2038        $this->Session->write('check1', $user['User']['id']);
2039    }
2040
2041    public function beforeFilter(): void {
2042        $this->loadModel('User');
2043        $this->loadModel('Activate');
2044        $this->loadModel('Tsumego');
2045        $this->loadModel('TsumegoRatingAttempt');
2046        $this->loadModel('Set');
2047        $this->loadModel('TimeModeAttempt');
2048        $this->loadModel('TsumegoStatus');
2049        $this->loadModel('Comment');
2050        $this->loadModel('TsumegoAttempt');
2051        $this->loadModel('AdminActivity');
2052        $this->loadModel('Achievement');
2053        $this->loadModel('AchievementStatus');
2054        $this->loadModel('AchievementCondition');
2055        $this->loadModel('SetConnection');
2056        $this->loadModel('Tag');
2057        $this->loadModel('TagName');
2058        $this->loadModel('Favorite');
2059
2060        Auth::init();
2061        $timeMode = new TimeMode();
2062
2063        $highscoreLink = 'highscore';
2064        $lightDark = 'light';
2065        $resetCookies = false;
2066        $levelBar = 1;
2067        $lastProfileLeft = 1;
2068        $lastProfileRight = 2;
2069        $hasFavs = false;
2070
2071        if (Auth::isLoggedIn()) {
2072            if (isset($_COOKIE['addTag']) && $_COOKIE['addTag'] != 0 && $this->Session->read('page') != 'set') {
2073                $newAddTag = explode('-', $_COOKIE['addTag']);
2074                $tagId = $newAddTag[0];
2075                $newTagName = $this->TagName->find('first', ['conditions' => ['name' => str_replace($tagId . '-', '', $_COOKIE['addTag'])]]);
2076                if ($newTagName) {
2077                    $saveTag = [];
2078                    $saveTag['Tag']['tag_name_id'] = $newTagName['TagName']['id'];
2079                    $saveTag['Tag']['tsumego_id'] = $tagId;
2080                    $saveTag['Tag']['user_id'] = Auth::getUserID();
2081                    $saveTag['Tag']['approved'] = 0;
2082                    $this->Tag->save($saveTag);
2083                }
2084                $this->set('removeCookie', 'addTag');
2085            }
2086            if (isset($_COOKIE['z_sess']) && $_COOKIE['z_sess'] != 0
2087            && strlen($_COOKIE['z_sess']) > 5) {
2088                Auth::getUser()['_sessid'] = $_COOKIE['z_sess'];
2089                Auth::saveUser();
2090            }
2091            if (Auth::getUser()['lastHighscore'] == 1) {
2092                $highscoreLink = 'highscore';
2093            } elseif (Auth::getUser()['lastHighscore'] == 2) {
2094                $highscoreLink = 'rating';
2095            } elseif (Auth::getUser()['lastHighscore'] == 3) {
2096                $highscoreLink = 'leaderboard';
2097            } elseif (Auth::getUser()['lastHighscore'] == 4) {
2098                $highscoreLink = 'highscore3';
2099            }
2100
2101            if (isset($_COOKIE['lastMode']) && $_COOKIE['lastMode'] != 0) {
2102                Auth::getUser()['lastMode'] = $_COOKIE['lastMode'];
2103                Auth::saveUser();
2104            }
2105            if (isset($_COOKIE['sound']) && $_COOKIE['sound'] != '0') {
2106                Auth::getUser()['sound'] = $_COOKIE['sound'];
2107                Auth::saveUser();
2108                unset($_COOKIE['sound']);
2109            }
2110            $this->set('ac', true);
2111            $this->set('user', Auth::getUser());
2112        }
2113
2114        if (isset($_COOKIE['lightDark']) && $_COOKIE['lightDark'] != '0') {
2115            $lightDark = $_COOKIE['lightDark'];
2116            if (Auth::isLoggedIn()) {
2117                // Convert string to integer for database storage
2118                $lightDarkInt = ($lightDark === 'light') ? 0 : 2;
2119                Auth::getUser()['lastLight'] = $lightDarkInt;
2120            }
2121        } elseif (Auth::isLoggedIn()) {
2122            if (Auth::getUser()['lastLight'] == 0
2123            || Auth::getUser()['lastLight'] == 1) {
2124                $lightDark = 'light';
2125            } else {
2126                $lightDark = 'dark';
2127            }
2128        }
2129
2130        if (Auth::isLoggedIn()) {
2131            $this->handleSearchSettings(Auth::getUserID());
2132            if (isset($_COOKIE['levelBar']) && $_COOKIE['levelBar'] != '0') {
2133                $levelBar = $_COOKIE['levelBar'];
2134                Auth::getUser()['levelBar'] = $levelBar;
2135            } elseif (Auth::getUser()['levelBar'] == 0
2136          || Auth::getUser()['levelBar'] == 'level') {
2137                $levelBar = 1;
2138            } else {
2139                $levelBar = 2;
2140            }
2141
2142            if (isset($_COOKIE['lastProfileLeft']) && $_COOKIE['lastProfileLeft'] != '0') {
2143                $lastProfileLeft = $_COOKIE['lastProfileLeft'];
2144                Auth::getUser()['lastProfileLeft'] = $lastProfileLeft;
2145            } else {
2146                $lastProfileLeft = Auth::getUser()['lastProfileLeft'];
2147                if ($lastProfileLeft == 0) {
2148                    $lastProfileLeft = 1;
2149                }
2150            }
2151            if (isset($_COOKIE['lastProfileRight']) && $_COOKIE['lastProfileRight'] != '0') {
2152                $lastProfileRight = $_COOKIE['lastProfileRight'];
2153                Auth::getUser()['lastProfileRight'] = $lastProfileRight;
2154            } else {
2155                $lastProfileRight = Auth::getUser()['lastProfileRight'];
2156                if ($lastProfileRight == 0) {
2157                    $lastProfileRight = 1;
2158                }
2159            }
2160        }
2161        $mode = 1;
2162        if (isset($_COOKIE['mode']) && $_COOKIE['mode'] != '0') {
2163            if ($_COOKIE['mode'] == 1) {
2164                $mode = 1;
2165            } else {
2166                $mode = 2;
2167            }
2168        }
2169
2170        if (Auth::isLoggedIn() && Auth::getUser()['mode'] == 2) {
2171            $mode = 2;
2172        }
2173
2174        if ($_COOKIE['sprint'] != 1) {
2175            $this->updateSprintCondition();
2176        }
2177
2178        if (Auth::isLoggedIn()) {
2179            if (isset($_COOKIE['revelation']) && $_COOKIE['revelation'] != 0) {
2180                Auth::getUser()['revelation'] -= 1;
2181            }
2182
2183            if (!$this->request->is('ajax')) {
2184                $this->PlayResultProcessor->checkPreviousPlay($timeMode);
2185            }
2186        }
2187        $boardNames = [];
2188        $enabledBoards = [];
2189        $boardPositions = [];
2190
2191        $boardNames[1] = 'Pine';
2192        $boardNames[2] = 'Ash';
2193        $boardNames[3] = 'Maple';
2194        $boardNames[4] = 'Shin Kaya';
2195        $boardNames[5] = 'Birch';
2196        $boardNames[6] = 'Wenge';
2197        $boardNames[7] = 'Walnut';
2198        $boardNames[8] = 'Mahogany';
2199        $boardNames[9] = 'Blackwood';
2200        $boardNames[10] = 'Marble 1';
2201        $boardNames[11] = 'Marble 2';
2202        $boardNames[12] = 'Marble 3';
2203        $boardNames[13] = 'Tibet Spruce';
2204        $boardNames[14] = 'Marble 4';
2205        $boardNames[15] = 'Marble 5';
2206        $boardNames[16] = 'Quarry 1';
2207        $boardNames[17] = 'Flowers';
2208        $boardNames[18] = 'Nova';
2209        $boardNames[19] = 'Spring';
2210        $boardNames[20] = 'Moon';
2211        $boardNames[21] = 'Apex';
2212        $boardNames[22] = 'Gold 1';
2213        $boardNames[23] = 'Amber';
2214        $boardNames[24] = 'Marble 6';
2215        $boardNames[25] = 'Marble 7';
2216        $boardNames[26] = 'Marble 8';
2217        $boardNames[27] = 'Marble 9';
2218        $boardNames[28] = 'Marble 10';
2219        $boardNames[29] = 'Jade';
2220        $boardNames[30] = 'Quarry 2';
2221        $boardNames[31] = 'Black Bricks';
2222        $boardNames[32] = 'Wallpaper 1';
2223        $boardNames[33] = 'Wallpaper 2';
2224        $boardNames[34] = 'Gold & Gray';
2225        $boardNames[35] = 'Gold & Pink';
2226        $boardNames[36] = 'Veil';
2227        $boardNames[37] = 'Tiles';
2228        $boardNames[38] = 'Mars';
2229        $boardNames[39] = 'Pink Cloud';
2230        $boardNames[40] = 'Reptile';
2231        $boardNames[41] = 'Mezmerizing';
2232        $boardNames[42] = 'Magenta Sky';
2233        $boardNames[43] = 'Tsumego Hero';
2234        $boardNames[44] = 'Pretty';
2235        $boardNames[45] = 'Hunting';
2236        $boardNames[46] = 'Haunted';
2237        $boardNames[47] = 'Carnage';
2238        $boardNames[48] = 'Blind Spot';
2239        $boardNames[49] = 'Giants';
2240        $boardNames[50] = 'Gems';
2241        $boardNames[51] = 'Grandmaster';
2242        $boardPositions[1] = [1, 'texture1', 'black34.png', 'white34.png'];
2243        $boardPositions[2] = [2, 'texture2', 'black34.png', 'white34.png'];
2244        $boardPositions[3] = [3, 'texture3', 'black34.png', 'white34.png'];
2245        $boardPositions[4] = [4, 'texture4', 'black.png', 'white.png'];
2246        $boardPositions[5] = [5, 'texture5', 'black34.png', 'white34.png'];
2247        $boardPositions[6] = [6, 'texture6', 'black.png', 'white.png'];
2248        $boardPositions[7] = [7, 'texture7', 'black34.png', 'white34.png'];
2249        $boardPositions[8] = [8, 'texture8', 'black.png', 'white.png'];
2250        $boardPositions[9] = [9, 'texture9', 'black.png', 'white.png'];
2251        $boardPositions[10] = [10, 'texture10', 'black34.png', 'white34.png'];
2252        $boardPositions[11] = [11, 'texture11', 'black34.png', 'white34.png'];
2253        $boardPositions[12] = [12, 'texture12', 'black34.png', 'white34.png'];
2254        $boardPositions[13] = [13, 'texture13', 'black34.png', 'white34.png'];
2255        $boardPositions[14] = [14, 'texture14', 'black34.png', 'white34.png'];
2256        $boardPositions[15] = [15, 'texture15', 'black.png', 'white.png'];
2257        $boardPositions[16] = [16, 'texture16', 'black34.png', 'white34.png'];
2258        $boardPositions[17] = [17, 'texture17', 'black34.png', 'white34.png'];
2259        $boardPositions[18] = [18, 'texture18', 'black.png', 'white.png'];
2260        $boardPositions[19] = [19, 'texture19', 'black34.png', 'white34.png'];
2261        $boardPositions[20] = [20, 'texture20', 'black34.png', 'white34.png'];
2262        $boardPositions[21] = [33, 'texture33', 'black34.png', 'white34.png'];
2263        $boardPositions[22] = [21, 'texture21', 'black.png', 'whiteKo.png'];
2264        $boardPositions[23] = [22, 'texture22', 'black34.png', 'white34.png'];
2265        $boardPositions[24] = [34, 'texture34', 'black.png', 'white.png'];
2266        $boardPositions[25] = [35, 'texture35', 'black34.png', 'white34.png'];
2267        $boardPositions[26] = [36, 'texture36', 'black.png', 'white.png'];
2268        $boardPositions[27] = [37, 'texture37', 'black34.png', 'white34.png'];
2269        $boardPositions[28] = [38, 'texture38', 'black38.png', 'white34.png'];
2270        $boardPositions[29] = [39, 'texture39', 'black.png', 'white.png'];
2271        $boardPositions[30] = [40, 'texture40', 'black34.png', 'white34.png'];
2272        $boardPositions[31] = [41, 'texture41', 'black34.png', 'white34.png'];
2273        $boardPositions[32] = [42, 'texture42', 'black34.png', 'white42.png'];
2274        $boardPositions[33] = [43, 'texture43', 'black34.png', 'white42.png'];
2275        $boardPositions[34] = [44, 'texture44', 'black34.png', 'white34.png'];
2276        $boardPositions[35] = [45, 'texture45', 'black34.png', 'white42.png'];
2277        $boardPositions[36] = [47, 'texture47', 'black34.png', 'white34.png'];
2278        $boardPositions[37] = [48, 'texture48', 'black34.png', 'white34.png'];
2279        $boardPositions[38] = [49, 'texture49', 'black.png', 'white.png'];
2280        $boardPositions[39] = [50, 'texture50', 'black34.png', 'white34.png'];
2281        $boardPositions[40] = [51, 'texture51', 'black34.png', 'white34.png'];
2282        $boardPositions[41] = [52, 'texture52', 'black34.png', 'white34.png'];
2283        $boardPositions[42] = [53, 'texture53', 'black34.png', 'white34.png'];
2284        $boardPositions[43] = [54, 'texture54', 'black54.png', 'white54.png'];
2285        $boardPositions[44] = [23, 'texture23', 'black.png', 'whiteFlower.png'];
2286        $boardPositions[45] = [24, 'texture24', 'black24.png', 'white24.png'];
2287        $boardPositions[46] = [25, 'texture25', 'blackGhost.png', 'white.png'];
2288        $boardPositions[47] = [26, 'texture26', 'blackInvis.png', 'whiteCarnage.png'];
2289        $boardPositions[48] = [27, 'texture27', 'black27.png', 'white27.png'];
2290        $boardPositions[49] = [28, 'texture28', 'blackGiant.png', 'whiteKo.png'];
2291        $boardPositions[50] = [29, 'texture29', 'blackKo.png', 'whiteKo.png'];
2292        $boardPositions[51] = [30, 'texture55', 'blackGalaxy.png', 'whiteGalaxy.png'];
2293
2294        $boardCount = 51;
2295
2296        if ($this->Session->check('texture') || (isset($_COOKIE['texture']) && $_COOKIE['texture'] != '0')) {
2297            $splitCookie = [];
2298            if (isset($_COOKIE['texture']) && $_COOKIE['texture'] != '0') {
2299                $splitCookie = str_split($_COOKIE['texture']);
2300                $textureCookies = $_COOKIE['texture'];
2301                Auth::getUser()['texture'] = $this->Session->read('texture');
2302                $this->Session->write('texture', $_COOKIE['texture']);
2303                $this->set('textureCookies', $textureCookies);
2304            } else {
2305                if (Auth::isLoggedIn()) {
2306                    $this->Session->write('texture', Auth::getUser()['texture']);
2307                }
2308                $textureCookies = $this->Session->read('texture');
2309                $splitTextureCookies = str_split($textureCookies);
2310                $splitTextureCookiesCount = count($splitTextureCookies);
2311                for ($i = 0; $i < $splitTextureCookiesCount; $i++) {
2312                    if ($splitTextureCookies[$i] == 2) {
2313                        $enabledBoards[$i + 1] = 'checked';
2314                    } else {
2315                        $enabledBoards[$i + 1] = '';
2316                    }
2317                }
2318            }
2319
2320            $splitCookieCount = count($splitCookie);
2321            for ($i = 0; $i < $splitCookieCount; $i++) {
2322                if ($splitCookie[$i] == 2) {
2323                    $enabledBoards[$i + 1] = 'checked';
2324                } else {
2325                    $enabledBoards[$i + 1] = '';
2326                }
2327            }
2328            if (Auth::isLoggedIn()) {
2329                Auth::saveUser();
2330            }
2331        }
2332
2333        if (!$this->Session->check('texture')) {
2334            $this->Session->write('texture', '222222221111111111111111111111111111111111111111111');
2335            $enabledBoards[1] = 'checked';
2336            $enabledBoards[2] = 'checked';
2337            $enabledBoards[3] = 'checked';
2338            $enabledBoards[4] = 'checked';
2339            $enabledBoards[5] = 'checked';
2340            $enabledBoards[6] = 'checked';
2341            $enabledBoards[7] = 'checked';
2342            $enabledBoards[8] = 'checked';
2343            $enabledBoards[9] = '';
2344            $enabledBoards[10] = '';
2345            $enabledBoards[11] = '';
2346            $enabledBoards[12] = '';
2347            $enabledBoards[13] = '';
2348            $enabledBoards[14] = '';
2349            $enabledBoards[15] = '';
2350            $enabledBoards[16] = '';
2351            $enabledBoards[17] = '';
2352            $enabledBoards[18] = '';
2353            $enabledBoards[19] = '';
2354            $enabledBoards[20] = '';
2355            $enabledBoards[21] = '';
2356            $enabledBoards[22] = '';
2357            $enabledBoards[23] = '';
2358            $enabledBoards[24] = '';
2359            $enabledBoards[25] = '';
2360            $enabledBoards[26] = '';
2361            $enabledBoards[27] = '';
2362            $enabledBoards[28] = '';
2363            $enabledBoards[29] = '';
2364            $enabledBoards[30] = '';
2365            $enabledBoards[31] = '';
2366            $enabledBoards[32] = '';
2367            $enabledBoards[33] = '';
2368            $enabledBoards[34] = '';
2369            $enabledBoards[35] = '';
2370            $enabledBoards[36] = '';
2371            $enabledBoards[37] = '';
2372            $enabledBoards[38] = '';
2373            $enabledBoards[39] = '';
2374            $enabledBoards[40] = '';
2375            $enabledBoards[41] = '';
2376            $enabledBoards[42] = '';
2377            $enabledBoards[43] = '';
2378            $enabledBoards[44] = '';
2379            $enabledBoards[45] = '';
2380            $enabledBoards[46] = '';
2381            $enabledBoards[47] = '';
2382            $enabledBoards[48] = '';
2383            $enabledBoards[49] = '';
2384            $enabledBoards[50] = '';
2385            $enabledBoards[51] = '';
2386        }
2387        $achievementUpdate = [];
2388        if ($this->Session->check('initialLoading')) {
2389            $achievementUpdate1 = $this->checkLevelAchievements();
2390            $achievementUpdate2 = $this->checkProblemNumberAchievements();
2391            $achievementUpdate3 = $this->checkRatingAchievements();
2392            $achievementUpdate4 = $this->checkTimeModeAchievements();
2393            $achievementUpdate5 = $this->checkDanSolveAchievements();
2394            $achievementUpdate = array_merge(
2395                $achievementUpdate1 ?: [],
2396                $achievementUpdate2 ?: [],
2397                $achievementUpdate3 ?: [],
2398                $achievementUpdate4 ?: [],
2399                $achievementUpdate5 ?: []
2400            );
2401            $this->Session->delete('initialLoading');
2402        }
2403
2404        if (count($achievementUpdate) > 0) {
2405            $this->updateXP(Auth::getUserID(), $achievementUpdate);
2406        }
2407
2408        $nextDay = new DateTime('tomorrow');
2409        if (Auth::isLoggedIn()) {
2410            Auth::getUser()['name'] = $this->checkPicture(Auth::getUser());
2411            $this->set('user', Auth::getUser());
2412        }
2413        $this->set('mode', $mode);
2414        $this->set('nextDay', $nextDay->format('m/d/Y'));
2415        $this->set('boardNames', $boardNames);
2416        $this->set('enabledBoards', $enabledBoards);
2417        $this->set('boardPositions', $boardPositions);
2418        $this->set('highscoreLink', $highscoreLink);
2419        $this->set('achievementUpdate', $achievementUpdate);
2420        $this->set('lightDark', $lightDark);
2421        $this->set('levelBar', $levelBar);
2422        $this->set('lastProfileLeft', $lastProfileLeft);
2423        $this->set('lastProfileRight', $lastProfileRight);
2424        $this->set('resetCookies', $resetCookies);
2425        $this->set('hasFavs', $hasFavs);
2426    }
2427
2428    public function afterFilter() {}
2429}